version 15.1
clear
clear matrix
clear mata
set more off
set maxvar 9000
use "C:\Users\Andy baker\OneDrive - UCB-O365\Stata\Barry\Final\OmnibusBothWide.dta", clear
drop if suspect1==1 | suspect2==1 | suspect3==1

*-----------------------------------CHANGED VOTE (DV)-------------------------------------------------
		*Of voters with candidate preferences (excludes DK from denominator, and also none/abstention and nr)
			*in both waves (1 and 2; 2 and 3)
gen change12=0 if respond1==1 & respond2==1 & v22c_presvote2~=3 & v22c_presvote1~=3 & v22c_presvote1~=6 & v22c_presvote1~=9 & v22c_presvote1<17 & v22c_presvote2<17
replace change12=1 if v22c_presvote1~=v22c_presvote2 & change12==0
bysort change12: tab v22c_presvote1 v22c_presvote2, m
tab change12

gen change23=0 if respond2==1 & respond3==1 & v22c_presvote2~=3 & v22c_presvote3~=3 & v22c_presvote2~=6 & v22c_presvote2~=9 & v22c_presvote2<17 & v22c_presvote3<17
replace change23=1 if v22c_presvote2~=v22c_presvote3 & change23==0
bysort change23: tab v22c_presvote2 v22c_presvote3, m
tab change23

gen change13=0 if respond1==1 & respond3==1 & v22c_presvote3~=3 & v22c_presvote1~=3 & v22c_presvote1~=6 & v22c_presvote1~=9 & v22c_presvote1<17 & v22c_presvote3<17
replace change13=1 if v22c_presvote1~=v22c_presvote3 & change13==0
bysort change13: tab v22c_presvote1 v22c_presvote3, m
tab change13

		*Strategic voting stuff for chapter 3 (note that these labels are meant for plurality in Mexico and aren't correct, but they are correct in the text. )
gen major_party_not_strategic13=1 if (v22c_presvote1==2 & v22c_presvote3==4) | (v22c_presvote1==4 & v22c_presvote3==2)
replace major_party_not_strategic13=0 if change13~=. & major_party_not_strategic13==.
*gen strategic13=1 if (v22c_presvote1==1 & v22c_presvote3==4) |  (v22c_presvote1==5 & v22c_presvote3==4) | (v22c_presvote1==1 & v22c_presvote3==2) |  (v22c_presvote1==5 & v22c_presvote3==2) | (v22c_presvote1==16 & v22c_presvote3==4) |  (v22c_presvote1==16 & v22c_presvote3==2) 
*replace strategic13=0 if change13~=. & strategic13==.
*old one: gen strategic13inbrazil=1 if (v22c_presvote1==2 & v22c_presvote3==1) |  (v22c_presvote1==4 & v22c_presvote3==1) | (v22c_presvote1==2 & v22c_presvote3==5) |  (v22c_presvote1==4 & v22c_presvote3==5)  
gen strategic13inbrazil=1 if (v22c_presvote1==2 & v22c_presvote3==4) |  (v22c_presvote1==2 & v22c_presvote3==5) | (v22c_presvote1==1 & v22c_presvote3==4) |  (v22c_presvote1==1 & v22c_presvote3==5)  
replace strategic13inbrazil=0 if change13~=. & strategic13inbrazil==.
	*SE cell is percent of switchers switching between top two, couldn't be strategic
tab change13 major_party_not_strategic13, row
	*SE cell is percent of switchers possibly strategic
*tab change13 strategic13, row cel
	*SE cell is percent of switchers possibly strategic in brazil
tab change13 strategic13inbrazil, row cel

gen major_party_not_strategic23=1 if (v22c_presvote2==2 & v22c_presvote3==4) | (v22c_presvote2==4 & v22c_presvote3==2)
replace major_party_not_strategic23=0 if change23~=. & major_party_not_strategic23==.
*gen strategic23=1 if (v22c_presvote2==1 & v22c_presvote3==4) |  (v22c_presvote2==5 & v22c_presvote3==4) | (v22c_presvote2==1 & v22c_presvote3==2) |  (v22c_presvote2==5 & v22c_presvote3==2) | (v22c_presvote2==16 & v22c_presvote3==4) |  (v22c_presvote2==16 & v22c_presvote3==2) 
*replace strategic23=0 if change23~=. & strategic23==.
gen strategic23inbrazil=1 if (v22c_presvote2==2 & v22c_presvote3==4) |  (v22c_presvote2==2 & v22c_presvote3==5) | (v22c_presvote2==1 & v22c_presvote3==4) |  (v22c_presvote2==1 & v22c_presvote3==5)  
replace strategic23inbrazil=0 if change23~=. & strategic23inbrazil==.
gen switchtolula23=(v22c_presvote2==1 & v22c_presvote3==2)   
replace switchtolula23=0 if change23~=. & switchtolula23==.
	*SE cell is percent of switchers switching between top two, couldn't be strategic
tab change23 major_party_not_strategic23, row
	*SE cell is percent of switchers possibly strategic
*tab change23 strategic23, row cel
	*SE cell is percent of switchers possibly strategic in brazil
tab change23 strategic23inbrazil, row cel
tab change23 switchtolula23, row cel

			*in all three waves (1-3)
gen changedatall=change12+change23
recode changedatall 2=1
tab changedatall
		
			*in both waves (5 and 6)		
gen change56=0 if respond5==1 & respond6==1 & v22c_presvote5~=9 & v22c_presvote5<17 & v22c_presvote6<17
replace change56=1 if v22c_presvote5~=v22c_presvote6 & change56==0
bysort change56: tab v22c_presvote5 v22c_presvote6, m
tab change56

		*Strategic voting stuff for chapter 3
gen major_party_not_strategic56=1 if (v22c_presvote5==2 & v22c_presvote6==10) | (v22c_presvote5==10 & v22c_presvote6==2)
replace major_party_not_strategic56=0 if change56~=. & major_party_not_strategic56==.
*gen strategic56=1 if (v22c_presvote5==11 & v22c_presvote6==2) |  (v22c_presvote5==11 & v22c_presvote6==10) | (v22c_presvote5==12 & v22c_presvote6==2) |  (v22c_presvote5==12 & v22c_presvote6==10)  | (v22c_presvote5==13 & v22c_presvote6==2) |  (v22c_presvote5==13 & v22c_presvote6==10) | (v22c_presvote5==14 & v22c_presvote6==2) |  (v22c_presvote5==14 & v22c_presvote6==10) | (v22c_presvote5==16 & v22c_presvote6==2) |  (v22c_presvote5==16 & v22c_presvote6==10)      
*replace strategic56=0 if change56~=. & strategic56==.
gen strategic56inbrazil=1 if (v22c_presvote5==2 & v22c_presvote6==11) |  (v22c_presvote5==10 & v22c_presvote6==11) | (v22c_presvote5==2 & v22c_presvote6==12) |  (v22c_presvote5==10 & v22c_presvote6==12)  
replace strategic56inbrazil=0 if change56~=. & strategic56inbrazil==.
gen helenatoalckmin56inbrazil=1 if (v22c_presvote5==12 & v22c_presvote6==10)   
replace helenatoalckmin56inbrazil=0 if change56~=. & helenatoalckmin56inbrazil==.
gen helenatolula56inbrazil=1 if (v22c_presvote5==12 & v22c_presvote6==2)   
replace helenatolula56inbrazil=0 if change56~=. & helenatolula56inbrazil==.

	*SE cell is percent of switchers switching between top two, couldn't be strategic
tab change56 major_party_not_strategic56, row
	*SE cell is percent of switchers possibly strategic
tab change56 strategic56, row cel
	*SE cell is percent of switchers possibly strategic in brazil
tab change56 strategic56inbrazil, row cel
tab change56 helenatoalckmin56inbrazil, row cel
tab change56 helenatolula56inbrazil, row cel	

	*INDECISOS
gen switch12=0 if respond1==1 & respond2==1 & v22c_presvote2~=3 & v22c_presvote1~=3 & v22c_presvote1~=6 & v22c_presvote1~=9 & v22c_presvote1~=. & v22c_presvote2<17
replace switch12=1 if v22c_presvote1~=v22c_presvote2 & v22c_presvote2<17 & switch12==0
replace switch12=2 if (v22c_presvote1==17 & switch12~=.) |  (v22c_presvote1==18 & switch12~=.) | (v22c_presvote1==19 & switch12~=.)   
bysort switch12: tab v22c_presvote1 v22c_presvote2, m
tab switch12

gen switch23=0 if respond2==1 & respond3==1 & v22c_presvote2~=3 & v22c_presvote3~=3 & v22c_presvote2~=6 & v22c_presvote2~=9 & v22c_presvote2~=. & v22c_presvote3<17
replace switch23=1 if v22c_presvote2~=v22c_presvote3 & v22c_presvote3<17 & switch23==0
replace switch23=2 if (v22c_presvote2==17 & switch23~=.) |  (v22c_presvote2==18 & switch23~=.) | (v22c_presvote3==19 & switch23~=.)   
bysort switch23: tab v22c_presvote2 v22c_presvote3, m
tab switch23

gen switch13=0 if respond1==1 & respond3==1 & v22c_presvote3~=3 & v22c_presvote1~=3 & v22c_presvote1~=6 & v22c_presvote1~=9 & v22c_presvote1~=. & v22c_presvote3<17
replace switch13=1 if v22c_presvote1~=v22c_presvote3 & v22c_presvote3<17 & switch13==0
replace switch13=2 if (v22c_presvote1==17 & switch13~=.) |  (v22c_presvote1==18 & switch13~=.) | (v22c_presvote1==19 & switch13~=.)   
bysort switch13: tab v22c_presvote1 v22c_presvote3, m
tab switch13

gen switch56=0 if respond5==1 & respond6==1 & v22c_presvote5~=9 & v22c_presvote6<17 & v22c_presvote5~=. 
replace switch56=1 if v22c_presvote5~=v22c_presvote6 & v22c_presvote6<17 & switch56==0
replace switch56=2 if (v22c_presvote5==17 & switch56~=.) |  (v22c_presvote5==18 & switch56~=.) | (v22c_presvote5==19 & switch56~=.)   
bysort switch56: tab v22c_presvote5 v22c_presvote6, m
tab switch56

			*in all three waves (1-3)
recode switch12-switch56 (2=1)
egen switchedatall=rsum(switch12 switch23)
replace switchedatall=. if respond1==0 | respond2==0 | respond3==0
replace switchedatall=. if v22c_presvote3>16
replace switchedatall=. if v22c_presvote1==3 | v22c_presvote1==6 | v22c_presvote1==9 | v22c_presvote1==16
replace switchedatall=. if v22c_presvote2==3 | v22c_presvote2==6 | v22c_presvote2==9
replace switchedatall=. if v22c_presvote1==17 & switchedatall==0
replace switchedatall=. if v22c_presvote1==18 & switchedatall==0
replace switchedatall=. if v22c_presvote1==. | v22c_presvote2==. | v22c_presvote3==. 
tab switchedatall
recode switchedatall 2=1


*--------------------------------NETWORK AND DISCUSSANT MEASURES-----------------------------------------------
*COUNT DISCUSSANTS (IV)
egen disctotal2=rsum(v74_disc1named2 v75_disc2named2 v76_disc3named2)
replace disctotal2=. if respond2==0
*Important. Dropping fresh wave 2 respondents because they didn't do discussants
replace disctotal2=. if whennew2==2

egen disctotal5=rsum(v74_disc1named5 v75_disc2named5 v76_disc3named5)
replace disctotal5=. if respond5==0
gen disctotal2sq=disctotal2^2
gen disctotal3=disctotal2
gen disctotal5sq=disctotal5^2
gen disctotal6=disctotal5


*IMPUTING DISCUSSANT-VALIDATED VOTE!!!!! 
			*comment out these six rows to use perceptions of discussant vote. 
			*uncomment to use actual discussant vote when available	
replace v74c_disc1presvote3=d1v22c_presvote3 if d1_orderinmainrespondentlist3==1 & d1v22c_presvote3~=.
replace v75c_disc2presvote3=d1v22c_presvote3 if d1_orderinmainrespondentlist3==2 & d1v22c_presvote3~=.
replace v76c_disc3presvote3=d1v22c_presvote3 if d1_orderinmainrespondentlist3==3 & d1v22c_presvote3~=.

replace v74c_disc1presvote3=d2v22c_presvote3 if d2_orderinmainrespondentlist3==1 & d2v22c_presvote3~=. 
replace v75c_disc2presvote3=d2v22c_presvote3 if d2_orderinmainrespondentlist3==2 & d2v22c_presvote3~=.
replace v76c_disc3presvote3=d2v22c_presvote3 if d2_orderinmainrespondentlist3==3 & d2v22c_presvote3~=.

		*Some accounting things
recode v74c_disc1presvote* v75c_disc2presvote* v76c_disc3presvote* (99=.) 
replace v74c_disc1presvote2 =. if v74_disc1named2 ==0
replace v75c_disc2presvote2 =. if v75_disc2named2 ==0
replace v76c_disc3presvote2 =. if v76_disc3named2 ==0
replace v74c_disc1presvote2 =19 if v74_disc1named2 ==1 & v74c_disc1presvote2==. & respond2==1
replace v75c_disc2presvote2 =19 if v75_disc2named2 ==1 & v75c_disc2presvote2==. & respond2==1
replace v76c_disc3presvote2 =19 if v76_disc3named2 ==1 & v76c_disc3presvote2==. & respond2==1
replace v74c_disc1presvote3 =. if v74_disc1named2 ==0 | respond3==0
replace v75c_disc2presvote3 =. if v75_disc2named2 ==0 | respond3==0
replace v76c_disc3presvote3 =. if v76_disc3named2 ==0 | respond3==0
replace v74c_disc1presvote3 =19 if v74_disc1named2 ==1 & v74c_disc1presvote3==. & respond3==1
replace v75c_disc2presvote3 =19 if v75_disc2named2 ==1 & v75c_disc2presvote3==. & respond3==1
replace v76c_disc3presvote3 =19 if v76_disc3named2 ==1 & v76c_disc3presvote3==. & respond3==1

replace v74c_disc1presvote5 =. if v74_disc1named5 ==0
replace v75c_disc2presvote5 =. if v75_disc2named5 ==0
replace v76c_disc3presvote5 =. if v76_disc3named5 ==0
replace v74c_disc1presvote5 =19 if v74_disc1named5 ==1 & v74c_disc1presvote5==. & respond5==1
replace v75c_disc2presvote5 =19 if v75_disc2named5 ==1 & v75c_disc2presvote5==. & respond5==1
replace v76c_disc3presvote5 =19 if v76_disc3named5 ==1 & v76c_disc3presvote5==. & respond5==1
replace v74c_disc1presvote6 =. if v74_disc1named5 ==0 | respond3==0
replace v75c_disc2presvote6 =. if v75_disc2named5 ==0 | respond3==0
replace v76c_disc3presvote6 =. if v76_disc3named5 ==0 | respond3==0
replace v74c_disc1presvote6 =19 if v74_disc1named5 ==1 & v74c_disc1presvote6==. & respond6==1
replace v75c_disc2presvote6 =19 if v75_disc2named5 ==1 & v75c_disc2presvote6==. & respond6==1
replace v76c_disc3presvote6 =19 if v76_disc3named5 ==1 & v76c_disc3presvote6==. & respond6==1
recode v74c_disc1presvote6 9=16
recode v75c_disc2presvote6 9=16
recode v76c_disc3presvote6 9=16

***WAVE 2: DEFINING DISAGREEMENT AND HETEROGENEITY
recode v74c_disc1presvote2 v75c_disc2presvote2 v76c_disc3presvote2 (1=1) (17/19=.) (.=.) (else=0), gen(ciro21 ciro22 ciro23)
recode v74c_disc1presvote2 v75c_disc2presvote2 v76c_disc3presvote2 (2=1) (17/19=.) (.=.) (else=0), gen(lula21 lula22 lula23)
recode v74c_disc1presvote2 v75c_disc2presvote2 v76c_disc3presvote2 (4=1) (17/19=.) (.=.) (else=0), gen(serra21 serra22 serra23)
recode v74c_disc1presvote2 v75c_disc2presvote2 v76c_disc3presvote2 (5=1) (17/19=.) (.=.) (else=0), gen(ag21 ag22 ag23)
recode v74c_disc1presvote2 v75c_disc2presvote2 v76c_disc3presvote2 (6=1) (17/19=.) (.=.) (else=0), gen(itamar21 itamar22 itamar23)
recode v74c_disc1presvote2 v75c_disc2presvote2 v76c_disc3presvote2 (7=1) (17/19=.) (.=.) (else=0), gen(zemaria21 zemaria22 zemaria23)
recode v74c_disc1presvote2 v75c_disc2presvote2 v76c_disc3presvote2 (8=1) (17/19=.) (.=.) (else=0), gen(rcp21 rcp22 rcp23)
recode v74c_disc1presvote2 v75c_disc2presvote2 v76c_disc3presvote2 (16=1) (17/19=.) (.=.) (else=0), gen(other21 other22 other23)
recode v74c_disc1presvote2 v75c_disc2presvote2 v76c_disc3presvote2 (17/19=1) (1/16=0) (.=.) (else=0), gen(noop21 noop22 noop23)
egen totopinionateddisc2=rsum(ciro21 ciro22 ciro23 lula21 lula22 lula23 serra21 serra22 serra23 ag21 ag22 ag23 itamar21 itamar22 itamar23 zemaria21 zemaria22 zemaria23 rcp21 rcp22 rcp23 other21 other22 other23)
replace totopinionateddisc2=. if respond2~=1
*Important. Dropping fresh wave 2 respondents because they didn't do discussants
replace totopinionateddisc2 =. if whennew2==2

gen agree21=0
replace agree21=1 if v74c_disc1presvote2==v22c_presvote2
replace agree21=. if v22c_presvote2>16
replace agree21=. if v74c_disc1presvote2>16
gen agree22=0
replace agree22=1 if v75c_disc2presvote2==v22c_presvote2
replace agree22=. if v22c_presvote2>16
replace agree22=. if v75c_disc2presvote2>16
gen agree23=0
replace agree23=1 if v76c_disc3presvote2==v22c_presvote2
replace agree23=. if v22c_presvote2>16
replace agree23=. if v76c_disc3presvote2>16
replace agree21=. if respond2~=1
replace agree22=. if respond2~=1
replace agree23=. if respond2~=1

egen numagree2=rsum(agree21-agree23)
replace numagree2=. if respond2==0
replace numagree2=. if v22c_presvote2>16

		*paradox thing
egen numciros2=rsum(ciro21 ciro22 ciro23)
egen numlulas2=rsum(lula21 lula22 lula23)
egen numserras2=rsum(serra21 serra22 serra23)
egen numgaros2=rsum(ag21 ag22 ag23)
gen propciros2=numciros2/totopinionateddisc2 
gen proplulas2=numlulas2/totopinionateddisc2
gen propserras2=numserras2/totopinionateddisc2
gen propgaros2=numgaros2/totopinionateddisc2

summ ciro21 lula21 serra21 ag21 if city1==1 & totopinionateddisc2>0 & totopinionateddisc2<5
tab v22c_presvote2 if city1==1 & v22c_presvote2<9 & v22c_presvote2~=3
summ ciro21 lula21 serra21 ag21 if city1==2 & totopinionateddisc2>0 & totopinionateddisc2<5
tab v22c_presvote2 if city1==2 & v22c_presvote2<9 & v22c_presvote2~=3

/*
*svyset [pweight=totopinionateddisc2]
*svy: mean propciros2 proplulas2 propserras2 propgaros2 if city1==1

summ propciros2 proplulas2 propserras2 propgaros2 if city1==2
tab v22c_presvote2 if city1==2 & v22c_presvote2<9
*/


	*STRONG
		*DISAGREEMENT is presence of disagreement over candidates (Sokhey et al AJPS)
				*Note that it only looks at discussants with reported opinions
gen disagreementstrong2=1-numagree2/totopinionateddisc2
replace disagreementstrong2=0 if totopinionateddisc2==0
replace disagreementstrong2=. if v22c_presvote2>16
*Important. Dropping fresh wave 2 respondents because they didn't do discussants
replace disagreementstrong2=. if whennew2==2
replace disagreementstrong2=. if respond2==0

		*HETEROGENEITY 
recode agree21 0=1 1=0, gen(disagree21)
recode agree22 0=1 1=0, gen(disagree22)
recode agree23 0=1 1=0, gen(disagree23)
egen numdisagree2=rowtotal(disagree21 disagree22 disagree23)
forvalues i=1(1)3 {
replace ciro2`i'=. if agree2`i'==1
replace lula2`i'=. if agree2`i'==1
replace serra2`i'=. if agree2`i'==1
replace ag2`i'=. if agree2`i'==1
replace itamar2`i'=. if agree2`i'==1
replace zemaria2`i'=. if agree2`i'==1
replace rcp2`i'=. if agree2`i'==1
replace other2`i'=. if agree2`i'==1
}
egen cirosum_numerator2=rsum(ciro21 ciro22 ciro23 )
egen lulasum_numerator2=rsum(lula21 lula22 lula23)
egen serrasum_numerator2=rsum(serra21 serra22 serra23)
egen agsum_numerator2=rsum(ag21 ag22 ag23)
egen itamarsum_numerator2=rsum(itamar21 itamar22 itamar23)
egen zemariasum_numerator2=rsum(zemaria21 zemaria22 zemaria23)
egen rcpsum_numerator2=rsum(rcp21 rcp22 rcp23)
egen othersum_numerator2=rsum(other21 other22 other23)
gen cirodiscprop2=cirosum_numerator2 / numdisagree2
gen luladiscprop2= lulasum_numerator2 /numdisagree2
gen serradiscprop2=serrasum_numerator2 /numdisagree2
gen agdiscprop2= agsum_numerator2/numdisagree2
gen itamardiscprop2=itamarsum_numerator2/numdisagree2
gen zemariadiscprop2= zemariasum_numerator2/numdisagree2
gen rcpdiscprop2= rcpsum_numerator2/numdisagree2
gen otherdiscprop2=othersum_numerator2/numdisagree2
gen heterogeneitystrong2 = 1-(cirodiscprop2^2+luladiscprop2^2+serradiscprop2^2+agdiscprop2^2+itamardiscprop2^2+zemariadiscprop2^2+rcpdiscprop2^2+otherdiscprop2^2)
recode heterogeneitystrong2 .=0
replace heterogeneitystrong2 =. if numdisagree2<2 & numdisagree2~=. & disagreementstrong2==. 
replace heterogeneitystrong2 =. if respond2==0
drop disagree21 disagree22 disagree23  numdisagree2

		*SINGLETON
recode totopinionateddisc2 0=1 .=. else=0, gen(zerodiscstrong2)
replace zerodiscstrong2=. if whennew2==2
replace zerodiscstrong2=. if respond2==0

	*WEAK
		*DISAGREEMENT is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak2=1-numagree2/disctotal2
replace disagreementweak2=0 if disctotal2==0
replace disagreementweak2=. if v22c_presvote2>16
*Important. Dropping fresh wave 2 respondents because they didn't do discussants
replace disagreementweak2=. if whennew2==2
replace disagreementweak2=. if respond2==0

		*HETEROGENEITY 
replace agree21=0 if v74c_disc1presvote2>16 & v74_disc1named2==1 & v22c_presvote2<17
replace agree22=0 if v75c_disc2presvote2>16 & v75_disc2named2==1 & v22c_presvote2<17
replace agree23=0 if v76c_disc3presvote2>16 & v76_disc3named2==1 & v22c_presvote2<17

recode agree21 0=1 1=0, gen(disagree21)
recode agree22 0=1 1=0, gen(disagree22)
recode agree23 0=1 1=0, gen(disagree23)

egen numdisagree2=rowtotal(disagree21 disagree22 disagree23)
forvalues i=1(1)3 {
replace noop2`i'=. if disagree2`i'==0  
}

egen noopsum_numerator2=rsum(noop21 noop22 noop23)
gen cirodiscprop2weak=cirosum_numerator2 / numdisagree2
gen luladiscprop2weak= lulasum_numerator2 /numdisagree2
gen serradiscprop2weak=serrasum_numerator2 /numdisagree2
gen agdiscprop2weak= agsum_numerator2/numdisagree2
gen itamardiscprop2weak=itamarsum_numerator2/numdisagree2
gen zemariadiscprop2weak= zemariasum_numerator2/numdisagree2
gen rcpdiscprop2weak= rcpsum_numerator2/numdisagree2
gen otherdiscprop2weak=othersum_numerator2/numdisagree2
gen noopdiscprop2weak=noopsum_numerator2/numdisagree2
gen heterogeneityweak2 = 1-(cirodiscprop2weak^2+luladiscprop2weak^2+serradiscprop2weak^2+agdiscprop2weak^2+itamardiscprop2weak^2+zemariadiscprop2weak^2+rcpdiscprop2weak^2+otherdiscprop2weak^2+noopdiscprop2weak^2)
recode heterogeneityweak2 .=0
replace heterogeneityweak2 =. if numdisagree2<2 & numdisagree2~=. & disagreementweak2==. 
*Important. Dropping fresh wave 2 and 3 respondents because they didn't do discussants
replace heterogeneityweak2=. if whennew2==2
replace heterogeneityweak2=. if respond2==0

		*SINGLETONS
recode disctotal2 0=1 .=. else=0, gen(zerodiscweak2)
replace zerodiscweak2=. if respond2==0

***WAVE 3: DEFINING DISAGREEMENT AND HETEROGENEITY
recode v74c_disc1presvote3 v75c_disc2presvote3 v76c_disc3presvote3 (1=1) (17/19=.) (.=.) (else=0), gen(ciro31 ciro32 ciro33)
recode v74c_disc1presvote3 v75c_disc2presvote3 v76c_disc3presvote3 (2=1) (17/19=.) (.=.) (else=0), gen(lula31 lula32 lula33)
recode v74c_disc1presvote3 v75c_disc2presvote3 v76c_disc3presvote3 (4=1) (17/19=.) (.=.) (else=0), gen(serra31 serra32 serra33)
recode v74c_disc1presvote3 v75c_disc2presvote3 v76c_disc3presvote3 (5=1) (17/19=.) (.=.) (else=0), gen(ag31 ag32 ag33)
recode v74c_disc1presvote3 v75c_disc2presvote3 v76c_disc3presvote3 (6=1) (17/19=.) (.=.) (else=0), gen(itamar31 itamar32 itamar33)
recode v74c_disc1presvote3 v75c_disc2presvote3 v76c_disc3presvote3 (7=1) (17/19=.) (.=.) (else=0), gen(zemaria31 zemaria32 zemaria33)
recode v74c_disc1presvote3 v75c_disc2presvote3 v76c_disc3presvote3 (8=1) (17/19=.) (.=.) (else=0), gen(rcp31 rcp32 rcp33)
recode v74c_disc1presvote3 v75c_disc2presvote3 v76c_disc3presvote3 (16=1) (17/19=.) (.=.) (else=0), gen(other31 other32 other33)
recode v74c_disc1presvote3 v75c_disc2presvote3 v76c_disc3presvote3 (17/19=1) (1/16=0) (.=.) (else=0), gen(noop31 noop32 noop33)
egen totopinionateddisc3=rsum(ciro31 ciro32 ciro33 lula31 lula32 lula33 serra31 serra32 serra33 ag31 ag32 ag33 itamar31 itamar32 itamar33 zemaria31 zemaria32 zemaria33 rcp31 rcp32 rcp33 other31 other32 other33)
replace totopinionateddisc3=. if respond3~=1
*Important. Dropping fresh wave 2 and 3 respondents because they didn't do discussants
replace totopinionateddisc3 =. if whennew3==3


				*Note this uses wave 2 MR vote choice
gen agree31=0
replace agree31=1 if v74c_disc1presvote3==v22c_presvote2
replace agree31=. if v22c_presvote2>16
replace agree31=. if v74c_disc1presvote3>16
gen agree32=0
replace agree32=1 if v75c_disc2presvote3==v22c_presvote2
replace agree32=. if v22c_presvote2>16
replace agree32=. if v75c_disc2presvote3>16
gen agree33=0
replace agree33=1 if v76c_disc3presvote3==v22c_presvote2
replace agree33=. if v22c_presvote2>16
replace agree33=. if v76c_disc3presvote3>16
replace agree31=. if respond3~=1
replace agree32=. if respond3~=1
replace agree33=. if respond3~=1
egen numagree3=rsum(agree31-agree33)
replace numagree3=. if respond3==0
replace numagree3=. if v22c_presvote2>16

	*STRONG
		*DISAGREEMENT is presence of disagreement over candidates (Sokhey et al AJPS)
				*Note that it only looks at discussants with reported opinions
gen disagreementstrong3=1-numagree3/totopinionateddisc3
replace disagreementstrong3=0 if totopinionateddisc3==0
replace disagreementstrong3=. if v22c_presvote2>16
*Important. Dropping fresh wave 2 and 3 respondents because they didn't do discussants
replace disagreementstrong3=. if whennew2==2
replace disagreementstrong3=. if whennew3==3
replace disagreementstrong3=. if respond3==0
replace disagreementstrong3=. if respond2==0

		*HETEROGENEITY 
recode agree31 0=1 1=0, gen(disagree31)
recode agree32 0=1 1=0, gen(disagree32)
recode agree33 0=1 1=0, gen(disagree33)
egen numdisagree3=rowtotal(disagree31 disagree32 disagree33)
forvalues i=1(1)3 {
replace ciro3`i'=. if agree3`i'==1
replace lula3`i'=. if agree3`i'==1
replace serra3`i'=. if agree3`i'==1
replace ag3`i'=. if agree3`i'==1
replace itamar3`i'=. if agree3`i'==1
replace zemaria3`i'=. if agree3`i'==1
replace rcp3`i'=. if agree3`i'==1
replace other3`i'=. if agree3`i'==1
}
egen cirosum_numerator3=rsum(ciro31 ciro32 ciro33 )
egen lulasum_numerator3=rsum(lula31 lula32 lula33)
egen serrasum_numerator3=rsum(serra31 serra32 serra33)
egen agsum_numerator3=rsum(ag31 ag32 ag33)
egen itamarsum_numerator3=rsum(itamar31 itamar32 itamar33)
egen zemariasum_numerator3=rsum(zemaria31 zemaria32 zemaria33)
egen rcpsum_numerator3=rsum(rcp31 rcp32 rcp33)
egen othersum_numerator3=rsum(other31 other32 other33)
gen cirodiscprop3=cirosum_numerator3 / numdisagree3
gen luladiscprop3= lulasum_numerator3 /numdisagree3
gen serradiscprop3=serrasum_numerator3 /numdisagree3
gen agdiscprop3= agsum_numerator3/numdisagree3
gen itamardiscprop3=itamarsum_numerator3/numdisagree3
gen zemariadiscprop3= zemariasum_numerator3/numdisagree3
gen rcpdiscprop3= rcpsum_numerator3/numdisagree3
gen otherdiscprop3=othersum_numerator3/numdisagree3
gen heterogeneitystrong3 = 1-(cirodiscprop3^2+luladiscprop3^2+serradiscprop3^2+agdiscprop3^2+itamardiscprop3^2+zemariadiscprop3^2+rcpdiscprop3^2+otherdiscprop3^2)
recode heterogeneitystrong3 .=0
replace heterogeneitystrong3 =. if numdisagree3<2 & numdisagree3~=. & disagreementstrong3==.
replace heterogeneitystrong3 =. if respond3==0
replace heterogeneitystrong3 =. if respond2==0
 
drop disagree31 disagree32 disagree33  numdisagree3

		*SINGLETONS
recode totopinionateddisc3 0=1 .=. else=0, gen(zerodiscstrong3)
replace totopinionateddisc3 =. if whennew2==2
replace zerodiscstrong3=. if whennew3==3
replace zerodiscstrong3=. if whennew2==2
replace zerodiscstrong3=. if respond3==0
replace zerodiscstrong3=. if respond2==0

	*WEAK 
		*DISAGREEMENT is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak3=1-numagree3/disctotal3
replace disagreementweak3=0 if disctotal3==0
replace disagreementweak3=. if v22c_presvote2>16
*Important. Dropping fresh wave 2 and 3 respondents because they didn't do discussants
replace disagreementweak3=. if whennew2==2
replace disagreementweak3=. if whennew3==3

		*HETEROGENEITY 
replace agree31=0 if v74c_disc1presvote3>16 & v74_disc1named2==1 & v22c_presvote2<17
replace agree32=0 if v75c_disc2presvote3>16 & v75_disc2named2==1 & v22c_presvote2<17
replace agree33=0 if v76c_disc3presvote3>16 & v76_disc3named2==1 & v22c_presvote2<17

recode agree31 0=1 1=0, gen(disagree31)
recode agree32 0=1 1=0, gen(disagree32)
recode agree33 0=1 1=0, gen(disagree33)

egen numdisagree3=rowtotal(disagree31 disagree32 disagree33)
forvalues i=1(1)3 {
replace noop3`i'=. if disagree3`i'==0  
}

egen noopsum_numerator3=rsum(noop31 noop32 noop33)
gen cirodiscprop3weak=cirosum_numerator3 / numdisagree3
gen luladiscprop3weak= lulasum_numerator3 /numdisagree3
gen serradiscprop3weak=serrasum_numerator3 /numdisagree3
gen agdiscprop3weak= agsum_numerator3/numdisagree3
gen itamardiscprop3weak=itamarsum_numerator3/numdisagree3
gen zemariadiscprop3weak= zemariasum_numerator3/numdisagree3
gen rcpdiscprop3weak= rcpsum_numerator3/numdisagree3
gen otherdiscprop3weak=othersum_numerator3/numdisagree3
gen noopdiscprop3weak=noopsum_numerator3/numdisagree3
gen heterogeneityweak3 = 1-(cirodiscprop3weak^2+luladiscprop3weak^2+serradiscprop3weak^2+agdiscprop3weak^2+itamardiscprop3weak^2+zemariadiscprop3weak^2+rcpdiscprop3weak^2+otherdiscprop3weak^2+noopdiscprop3weak^2)
recode heterogeneityweak3 .=0
replace heterogeneityweak3 =. if numdisagree3<2 & numdisagree3~=. & disagreementweak3==. 
*Important. Dropping fresh wave 2 and 3 respondents because they didn't do discussants
replace heterogeneityweak3=. if whennew2==2
replace heterogeneityweak3=. if whennew2==3
replace heterogeneityweak3=. if respond3==0
replace heterogeneityweak3=. if respond2==0

		*SINGLETONS
*don't need because no change between 2 and 3. Just use zerodiscweak2

*-----------------------------------------------------------------------------------------------------------------------------------------
*STATS on changing in unison
recode v74c_disc1presvote2 v75c_disc2presvote2 v76c_disc3presvote2 (1=1) (17/19=.) (.=.) (else=0), gen(ciro21temp ciro22temp ciro23temp)
egen ciro_temp2=rowtotal(ciro21temp ciro22temp ciro23temp)
gen ciro_unam2=ciro_temp2/totopinionateddisc2
replace ciro_unam2=0 if ciro_unam2<1

recode v74c_disc1presvote3 v75c_disc2presvote3 v76c_disc3presvote3 (2=1) (17/19=.) (.=.) (else=0), gen(lula31temp lula32temp lula33temp)
recode v74c_disc1presvote3 v75c_disc2presvote3 v76c_disc3presvote3 (4=1) (17/19=.) (.=.) (else=0), gen(serra31temp serra32temp serra33temp)
recode v74c_disc1presvote3 v75c_disc2presvote3 v76c_disc3presvote3 (5=1) (17/19=.) (.=.) (else=0), gen(garo31temp garo32temp garo33temp)
egen lula_temp3=rowtotal(lula31temp lula32temp lula33temp)
egen serra_temp3=rowtotal(serra31temp serra32temp serra33temp)
egen garo_temp3=rowtotal(garo31temp garo32temp garo33temp)

gen lula_unam3=lula_temp3/totopinionateddisc3
gen serra_unam3=serra_temp3/totopinionateddisc3
gen garo_unam3=garo_temp3/totopinionateddisc3
	
egen unam3=rowmax(lula_unam3 serra_unam3 garo_unam3)
replace unam3=0 if unam3<1
	
tab ciro_unam2 unam3 if v22c_presvote2==1 & whennew1==1 & change23==1 & respond3==1,m

bysort change23: tab  unam3 if v22c_presvote2==1 & ciro_unam2==1  & whennew1==1,m
bysort change23: tab  unam3 if v22c_presvote2==1 & ciro_unam2==1 & v74c_disc1presvote3 ==v22c_presvote3 & whennew1==1,m 
display 42/(118+69)

*---------------------------------GET QUICK CROSSTABS FOR FIGURE WITH MARGINAL DIST'N OF NETWORK TRAITS-------------------------------
preserve
replace disagreementstrong2=-1 if zerodiscstrong2==1
tab disctotal2 if whennew1==1 & respond2==1 & v22c_presvote2<16
tab disagreementstrong2 if whennew1==1 & respond2==1 & v22c_presvote2<16
tab disagreementstrong2 heterogeneitystrong2 if whennew1==1 & respond2==1  & v22c_presvote2<16, cel
restore


*-----------------GENERATE MEDIA ATTENTION SCORE (IV).
replace v7b1fq_mediatv1choicefreq1=0 if v7a_mediatvyesorno1==2
replace v7b2fq_mediatv2choicefreq1=0 if v7a_mediatvyesorno1==2
replace v8b1fq_mediamags1choicefreq1=0 if v8a_mediamagsyesorno1==2
replace v8b2fq_mediamags2choicefreq1=0 if v8a_mediamagsyesorno1==2
replace v9b1fq_mediapaper1choicefreq1=0 if v9a_mediapaperyesorno1==2
replace v9b2fq_mediapaper2choicefreq1=0 if v9a_mediapaperyesorno1==2
recode v7b1fq_mediatv1choicefreq1 v7b2fq_mediatv2choicefreq1 v8b1fq_mediamags1choicefreq1 v8b2fq_mediamags2choicefreq1 v9b1fq_mediapaper1choicefreq1 v9b2fq_mediapaper2choicefreq1 (.=0)
mvdecode v7b1fq_mediatv1choicefreq1 v7b2fq_mediatv2choicefreq1 v8b1fq_mediamags1choicefreq1 v8b2fq_mediamags2choicefreq1 v9b1fq_mediapaper1choicefreq1 v9b2fq_mediapaper2choicefreq1, mv(8)
mvdecode v7b1fq_mediatv1choicefreq1 v7b2fq_mediatv2choicefreq1 v8b1fq_mediamags1choicefreq1 v8b2fq_mediamags2choicefreq1 v9b1fq_mediapaper1choicefreq1 v9b2fq_mediapaper2choicefreq1, mv(9)
factor  v7b1fq_mediatv1choicefreq1 v7b2fq_mediatv2choicefreq1 v8b1fq_mediamags1choicefreq1 v8b2fq_mediamags2choicefreq1 v9b1fq_mediapaper1choicefreq1 v9b2fq_mediapaper2choicefreq1 if respond1==1, pcf factors(1)
predict mediaattention1temp
impute mediaattention1temp v7b1fq_mediatv1choicefreq1 v7b2fq_mediatv2choicefreq1 v8b1fq_mediamags1choicefreq1 v8b2fq_mediamags2choicefreq1 v9b1fq_mediapaper1choicefreq1 v9b2fq_mediapaper2choicefreq1 if respond1==1, gen(mediaattention1)


replace v7b1fq_mediatv1choicefreq2=0 if v7a_mediatvyesorno2==2
replace v7b2fq_mediatv2choicefreq2=0 if v7a_mediatvyesorno2==2
replace v9b1fq_mediapaper1choicefreq2=0 if v9a_mediapaperyesorno2==2
replace v9b2fq_mediapaper2choicefreq2=0 if v9a_mediapaperyesorno2==2
recode  v7b1fq_mediatv1choicefreq2 v7b2fq_mediatv2choicefreq2 v9b1fq_mediapaper1choicefreq2 v9b2fq_mediapaper2choicefreq2 (.=0)
mvdecode v7b1fq_mediatv1choicefreq2 v7b2fq_mediatv2choicefreq2 v9b1fq_mediapaper1choicefreq2 v9b2fq_mediapaper2choicefreq2, mv(8)
mvdecode v7b1fq_mediatv1choicefreq2 v7b2fq_mediatv2choicefreq2 v9b1fq_mediapaper1choicefreq2 v9b2fq_mediapaper2choicefreq2, mv(9)
factor  v7b1fq_mediatv1choicefreq2 v7b2fq_mediatv2choicefreq2 v9b1fq_mediapaper1choicefreq2 v9b2fq_mediapaper2choicefreq2 if respond2==1, pcf factors(1)
predict mediaattention2temp
impute mediaattention2temp v7b1fq_mediatv1choicefreq2 v7b2fq_mediatv2choicefreq2 v9b1fq_mediapaper1choicefreq2 v9b2fq_mediapaper2choicefreq2 if respond2==1, gen(mediaattention2)


replace v7b1fq_mediatv1choicefreq3=0 if v7a_mediatvyesorno3==2
replace v7b2fq_mediatv2choicefreq3=0 if v7a_mediatvyesorno3==2
replace v8b1fq_mediamags1choicefreq3=0 if v8a_mediamagsyesorno3==2
replace v8b2fq_mediamags2choicefreq3=0 if v8a_mediamagsyesorno3==2
replace v9b1fq_mediapaper1choicefreq3=0 if v9a_mediapaperyesorno3==2
replace v9b2fq_mediapaper2choicefreq3=0 if v9a_mediapaperyesorno3==2
replace v10b_mediaradiofreq3=10 if v10b_mediaradiofreq3>10 & v10b_mediaradiofreq3<61
replace v10b_mediaradiofreq3=0 if v10a_mediaradioyesorno3==2
recode  v7b1fq_mediatv1choicefreq3 v7b2fq_mediatv2choicefreq3 v8b1fq_mediamags1choicefreq3 v8b2fq_mediamags2choicefreq3 v9b1fq_mediapaper1choicefreq3 v9b2fq_mediapaper2choicefreq3 v10b_mediaradiofreq3 (.=0)
mvdecode v7b1fq_mediatv1choicefreq3 v7b2fq_mediatv2choicefreq3 v8b1fq_mediamags1choicefreq3 v8b2fq_mediamags2choicefreq3 v9b1fq_mediapaper1choicefreq3 v9b2fq_mediapaper2choicefreq3 v10b_mediaradiofreq3, mv(8)
mvdecode v7b1fq_mediatv1choicefreq3 v7b2fq_mediatv2choicefreq3 v8b1fq_mediamags1choicefreq3 v8b2fq_mediamags2choicefreq3 v9b1fq_mediapaper1choicefreq3 v9b2fq_mediapaper2choicefreq3 v10b_mediaradiofreq3, mv(9)
factor  v7b1fq_mediatv1choicefreq3 v7b2fq_mediatv2choicefreq3 v8b1fq_mediamags1choicefreq3 v8b2fq_mediamags2choicefreq3 v9b1fq_mediapaper1choicefreq3 v9b2fq_mediapaper2choicefreq3 v10b_mediaradiofreq3 if respond3==1, pcf factors(1)
predict mediaattention3temp
impute mediaattention3temp v7b1fq_mediatv1choicefreq3 v7b2fq_mediatv2choicefreq3 v8b1fq_mediamags1choicefreq3 v8b2fq_mediamags2choicefreq3 v9b1fq_mediapaper1choicefreq3 v9b2fq_mediapaper2choicefreq3 v10b_mediaradiofreq3 if respond3==1, gen(mediaattention3)

factor mediaattention1 mediaattention2 mediaattention3, pcf factors(1)
predict mediaattentiontemp
impute mediaattentiontemp v7b1fq_mediatv1choicefreq1 v7b2fq_mediatv2choicefreq1 v8b1fq_mediamags1choicefreq1 v8b2fq_mediamags2choicefreq1 v9b1fq_mediapaper1choicefreq1 v9b2fq_mediapaper2choicefreq1 v7b1fq_mediatv1choicefreq2 v7b2fq_mediatv2choicefreq2 v9b1fq_mediapaper1choicefreq2 v9b2fq_mediapaper2choicefreq2  v7b1fq_mediatv1choicefreq3 v7b2fq_mediatv2choicefreq3 v8b1fq_mediamags1choicefreq3 v8b2fq_mediamags2choicefreq3 v9b1fq_mediapaper1choicefreq3 v9b2fq_mediapaper2choicefreq3 v10b_mediaradiofreq3 , gen(mediaattention)


*----------------------------------------PARTISAN INTENSITY
recode  v43a_pidyesorno1 2=0 .=0 1=1 else=0
replace v43a_pidyesorno1 =0 if v43b_pidparty1==.
replace v43a_pidyesorno1 =0 if v43b_pidparty1==21

recode  v44a_pidyesorno1 2=0 .=0 1=1 else=0
replace v44a_pidyesorno1=. if v43a_pidyesorno1==1
replace v44a_pidyesorno1 =0 if v44b_pidparty1==.
replace v44a_pidyesorno1 =0 if v44b_pidparty1==21

gen partyintense1=0
replace partyintense1=1 if v43a_pidyesorno1==1
replace partyintense1=.5 if v44a_pidyesorno1==1

*------------------------------------------THRESHOLD (note this introduces some missing data)
mvdecode v41a_thermlula* v41c_thermciro* v41d_thermgaro* v41e_thermserra* v41x_thermalckmin* v41y_thermheloisa* v41z_thermbuarque* v42a_pidthermpsdb* v42c_pidthermpt* v42e_pidthermpsb* v42f_pidthermpps*, mv(18)
mvdecode v41a_thermlula* v41c_thermciro* v41d_thermgaro* v41e_thermserra* v41x_thermalckmin* v41y_thermheloisa* v41z_thermbuarque* v42a_pidthermpsdb* v42c_pidthermpt* v42e_pidthermpsb* v42f_pidthermpps*, mv(19)

	*For Lula voters
egen tempciroserraag=rowmean(v41c_thermciro2 v41d_thermgaro2 v41e_thermserra2)
egen templula=rowmean(v41a_thermlula2)
gen lula_therm2=templula-tempciroserraag

	*For Ciro voters
egen templulaserraag=rowmean(v41a_thermlula2 v41d_thermgaro2 v41e_thermserra2)
egen tempciro=rowmean(v41c_thermciro2 )
gen ciro_therm2=tempciro-templulaserraag

	*For Serra voters
egen templulaagciro=rowmean(v41a_thermlula2 v41d_thermgaro2 v41c_thermciro2 )
egen tempserra=rowmean(v41e_thermserra2)
gen serra_therm2=tempserra-templulaagciro

	*For AG voters
egen templulaserraciro=rowmean(v41a_thermlula2 v41e_thermserra2 v41c_thermciro2 )
egen tempag=rowmean(v41d_thermgaro2)
gen ag_therm2=tempag-templulaserraciro

gen threshold2=lula_therm2 if v22c_presvote2==2
replace threshold2=ciro_therm2 if v22c_presvote2==1
replace threshold2=serra_therm2 if v22c_presvote2==4
replace threshold2=ag_therm2 if v22c_presvote2==5
*replace threshold2=0 if threshold2<0
replace threshold2=(threshold2+10)/20

*---------------------------AWARENESS   
gen correct1cs=0 if respond1==1 & city1==1
replace correct1cs=2 if s14acs_know1cargo1==1 & correct1cs==0 
		* Give half a point to people who said Ana Corso was a Fed. Dep. because she was a suplente for 8 months
replace correct1cs=1 if s14acs_know1cargo1==2 & correct1cs==0 
gen correct1jf=0 if respond1==1 & city1==2
replace correct1jf=1 if s14ajf_know1cargo1==2 & correct1jf==0 
gen correct2=0 if respond1==1
replace correct2=1 if s14b_know2vicepres1==2 & respond1==1
gen correct3=0 if respond1==1
replace correct3=1 if s14c_know3fhcparty1==3 & respond1==1
gen correct4=0 if respond1==1
replace correct4=1 if s14d_know4mercosul1==2 & respond1==1
gen correct5cs=0 if respond1==1 & city1==1
replace correct5cs=1 if s14ecs_know5senator1==1 & correct5cs==0
gen correct5jf=0 if respond1==1 & city1==2
replace correct5jf=1 if s14ejf_know5senator1==1 & correct5jf==0
gen correct6=0 if respond1==1
replace correct6=1 if s14f_know6preschamber1==3 & respond1==1

*this is for the fresh respondents
replace correct1cs=0 if respond2==1 & whennew1==2 & city1==1
replace correct1cs=2 if s14acs_know1cargo2==1 & respond2==1 & whennew1==2 & city1==1
replace correct1cs=1 if s14acs_know1cargo2==2 & respond2==1 & whennew1==2 & city1==1
replace correct1jf=0 if respond2==1 & whennew1==2 & city1==2
replace correct1jf=1 if s14ajf_know1cargo2==2 & respond2==1 & whennew1==2 & city1==2
replace correct2=0 if respond2==1 & whennew1==2 
replace correct2=1 if s14b_know2vicepres2==2 & respond2==1 & whennew1==2
replace correct3=0 if respond2==1 & whennew1==2 
replace correct3=1 if s14c_know3fhcparty2==3 & respond2==1 & whennew1==2
replace correct4=0 if respond2==1 & whennew1==2 
replace correct4=1 if s14d_know4mercosul2==2 & respond2==1 & whennew1==2
replace correct5cs=0 if respond2==1 & whennew1==2 & city1==1
replace correct5cs=1 if s14ecs_know5senator2==1 & respond2==1 & whennew1==2 & city1==1
replace correct5jf=0 if respond2==1 & whennew1==2 & city1==2
replace correct5jf=1 if s14ejf_know5senator2==1 & respond2==1 & whennew1==2 & city1==2
replace correct6=0 if respond2==1 & whennew1==2 
replace correct6=1 if s14f_know6preschamber2==3 & respond2==1 & whennew1==2

replace correct2=0 if respond3==1 & whennew1==3 
replace correct2=1 if s14b_know2vicepres3==2 & respond3==1 & whennew1==3
replace correct3=0 if respond3==1 & whennew1==3 
replace correct3=1 if s14c_know3fhcparty3==3 & respond3==1 & whennew1==3
replace correct4=0 if respond3==1 & whennew1==3 
replace correct4=1 if s14d_know4mercosul3==2 & respond3==1 & whennew1==3

gen correct7cs=0 if respond3==1 & city1==1
replace correct7cs=1 if s14gcs_know7mayor3==4
gen correct7jf=0 if respond3==1 & city1==2
replace correct7jf=1 if s14gjf_know7mayor3==4

irt grm correct1cs correct1jf correct2 correct3 correct4 correct5cs correct5jf correct6 correct7cs correct7jf
	*NOTE FOR NOW IT'S NOT SCALED SO THAT MEAN IS ZERO FOR FIRST WAVE
predict awaretemp , latent ebmeans
egen awarez=std(awaretemp) 

recode v39a_persuadetoothersyesorno* (2=0) (8=.) (9=.) (99=.)
logit v39a_persuadetoothersyesorno1 awarez 
logit v39a_persuadetoothersyesorno3 awarez 

*---------------------------------SIMPLE INDY LEVEL MODELS FOR FIGURES IN CHAPTER 4
bysort change23: summ awarez 
bysort change23 zerodiscstrong2: summ disagreementstrong2 
bysort change23 zerodiscweak2: summ disagreementweak2 
	
*eststo clear

*Figure 4.9B, Table A.11
logit changedatall awarez i.v22c_presvote1 i.v22c_presvote2 city1, cluster(bairroibge1)
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans

logit switchedatall awarez city1 , cluster(bairroibge1)
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans

	*timing
gen timing=1 if v22c_presvote1==v22c_presvote2 & v22c_presvote2==v22c_presvote3 & v22c_presvote1==v22c_presvote3 & v22c_presvote3~=. & v22c_presvote2~=. & v22c_presvote1~=.
replace timing=2 if v22c_presvote1~=v22c_presvote2 & v22c_presvote2==v22c_presvote3 & v22c_presvote3~=. & v22c_presvote2~=. & v22c_presvote1~=. 
replace timing=3 if v22c_presvote2~=v22c_presvote3 & v22c_presvote3~=. & v22c_presvote2~=. 

replace timing=. if v22c_presvote1==3 
replace timing=. if v22c_presvote1==6 
replace timing=. if v22c_presvote1==9
replace timing=. if v22c_presvote1==16
replace timing=. if v22c_presvote2==3
replace timing=. if v22c_presvote3>7
replace timing=. if v22c_presvote1==.
replace timing=. if responsepattern3wave~=7

recode v22c_presvote1 3=1 else=0, gen(roseana)
recode v22c_presvote1 6=1 else=0, gen(itamar)

*Table A.11
ologit timing awarez city1 , cluster(bairroibge1)
*ologit timing awarez city1 roseana itamar, cluster(bairroibge1)
summ awarez
gen awarebin=0 if awarez==r(min)
replace awarebin=1 if awarez==r(max)
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans
margin, at((min) awarez) atmeans
margin, at((max) awarez) atmeans

*Figure 4.9A
margin, at((max) awarez) at((min) awarez) atmeans

/*
marginsplot, xdimension(_outcome) yscale(range(0 .6)) ylab(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%") xscale(range(.8 3.2)) title("{bf:         Brazil 2002}", color(black) size(medsmall)) ///
ytitle("{bf:Model-Predicted}" "{bf:Percentage of Voters}") legend(off) graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) xlab(1 "Bef. campaign" 2 "Bef. mid-campaign" 3 "Bef. election day", labsize(small)) xtitle("{bf:Timing of Vote Decision}") ///
plot1opts(msymbol(0) lcolor(black) mcolor(black) ) plot2opts(msymbol(S) lcolor(black) mcolor(black) ) ci1opts(color(black)) ci2opts(color(black)) ///
text(.495 1.39 "Max knowledge", size(small)) text(.495 2.61 "Min knowledge", color(black) size(small)) text(0 1 "{it:N}=1,761", size(small)) 
graph save "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2002timing.gph", replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2002timing.tif", as(tif) replace
*/
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
*esttab est1 est2 using figure49a.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Vote Switching by Political Knowledge)

ci mean awarez if v22c_presvote3==1 & switchedatall==0 
ci mean awarez if v22c_presvote3==1 & switchedatall==1

ci mean awarez if v22c_presvote3==2 & switchedatall==0
ci mean awarez if v22c_presvote3==2 & switchedatall==1

ci mean awarez if v22c_presvote3==4 & switchedatall==0
ci mean awarez if v22c_presvote3==4 & switchedatall==1

ci mean awarez if v22c_presvote3==5 & switchedatall==0
ci mean awarez if v22c_presvote3==5 & switchedatall==1

*eststo clear
label variable disagreementstrong2 "Network disagreement"
label variable heterogeneitystrong2 "Disagreeing alters’ diversity"
label variable zerodiscstrong2 "Singleton"
label variable city1 "Juiz de Fora"

*eststo: logit change23 disagreementstrong2 heterogeneitystrong2 zerodiscstrong2 city1, cluster(bairroibge1)
*Figure 4.3, Table A.3
logit change23 disagreementstrong2 heterogeneitystrong2 zerodiscstrong2 city1, cluster(bairroibge1)
margin, at(disagreementstrong2=1 zerodiscstrong2==0 heterogeneitystrong2==0)
margin, at(disagreementstrong2=1 zerodiscstrong2==0 heterogeneitystrong2==.444444)
margin, at(disagreementstrong2=.6666666 zerodiscstrong2==0 heterogeneitystrong2==0)
margin, at(disagreementstrong2=0 zerodiscstrong2==1 heterogeneitystrong2==0)
margin, at(disagreementstrong2=0 zerodiscstrong2==0 heterogeneitystrong2==0)

*esttab est1 using figure43.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Changed Vote Preference between Campaign and Election Waves by Campaign-Wave Network Traits: Results from Four Binary Logit Models )

tab disagreementstrong2 heterogeneitystrong2 if change23~=. & whennew1==1 , cel m

*----------------------------------AGGREGATE 
preserve
drop if whennew2==2
drop if whennew3==3
	*Partisan intensity 
recode v43b_pidparty1 (4=1) (else=0), gen(partyintensePT1)
replace partyintensePT1=.5 if v44b_pidparty1==4
replace partyintensePT1=. if respond1==0
egen partyintensePT=mean(partyintensePT1), by(city1)

recode v43b_pidparty1 (3=1) (else=0), gen(partyintensePSDB1)
replace partyintensePSDB1=.5 if v44b_pidparty1==3
replace partyintensePSDB1=. if respond1==0
egen partyintensePSDB=mean(partyintensePSDB1), by(city1)

recode v43b_pidparty1 (13=1) (else=0), gen(partyintensePPS1)
replace partyintensePPS1=.5 if v44b_pidparty1==13
replace partyintensePPS1=. if respond1==0
egen partyintensePPS=mean(partyintensePPS1), by(city1)

recode v43b_pidparty1 (14=1) (else=0), gen(partyintensePSB1)
replace partyintensePSB1=.5 if v44b_pidparty1==14
replace partyintensePSB1=. if respond1==0
egen partyintensePSB=mean(partyintensePSB1), by(city1)

	*Stability
recode v22c_presvote2 v22c_presvote3 (17/19=.), gen(presvote2 presvote3) 
label values presvote2 presvote3 presvote
bysort city1: tab presvote2 presvote3, row
bysort v22c_presvote2: ci means disagreementstrong2 

gen ciro02stability=0 if presvote2==1 & presvote3~=.
replace ciro02stability=1 if presvote3==1 & presvote2==1

gen lula02stability=0 if presvote2==2 & presvote3~=.
replace lula02stability=1 if presvote3==2 & presvote2==2

gen serra02stability=0 if presvote2==4 & presvote3~=.
replace serra02stability=1 if presvote3==4 & presvote2==4

gen ag02stability=0 if presvote2==5 & presvote3~=.
replace ag02stability=1 if presvote3==5 & presvote2==5

*to get complement of their vote total in campaign wave
recode presvote2 1=1 .=. else=0, gen(cirovotetemp1)
egen cirovotetemp2=mean(cirovotetemp1)
gen cirovote=1-cirovotetemp2

recode presvote2 2=1 .=. else=0, gen(lulavotetemp1)
egen lulavotetemp2=mean(lulavotetemp1)
gen lulavote=1-lulavotetemp2

recode presvote2 4=1 .=. else=0, gen(serravotetemp1)
egen serravotetemp2=mean(serravotetemp1)
gen serravote=1-serravotetemp2

recode presvote2 5=1 .=. else=0, gen(garotinhovotetemp1)
egen garotinhovotetemp2=mean(garotinhovotetemp1)
gen garotinhovote=1-garotinhovotetemp2

replace lula_therm2=. if v22c_presvote2~=2
replace ciro_therm2=. if v22c_presvote2~=1
replace serra_therm2=. if v22c_presvote2~=4
replace ag_therm2=. if v22c_presvote2~=5
replace lulavote=. if v22c_presvote2~=2
replace cirovote=. if v22c_presvote2~=1
replace serravote=. if v22c_presvote2~=4
replace garotinhovote=. if v22c_presvote2~=5
replace partyintensePT=. if v22c_presvote2~=2
replace partyintensePSDB=. if v22c_presvote2~=4
replace partyintensePPS=. if v22c_presvote2~=1
replace partyintensePSB=. if v22c_presvote2~=5

collapse disagreementstrong2 lula_therm2 ciro_therm2 serra_therm2 ag_therm2 cirovote lulavote serravote garotinhovote lula02stability ciro02stability serra02stability ag02stability partyintensePT partyintensePSDB partyintensePPS partyintensePSB, by(presvote2 city1)
keep if presvote2<6 & presvote2~=3
egen threshold=rowmean(lula_therm2 ciro_therm2 serra_therm2 ag_therm2)
egen votecomplement=rowmean(cirovote lulavote serravote garotinhovote)
egen stability=rowmean(lula02stability ciro02stability serra02stability ag02stability )
egen partyintense=rowmean(partyintensePT partyintensePSDB partyintensePPS partyintensePSB)
gen year=2002
rename presvote2 presvote
rename disagreementstrong2 disagreementstrong
drop lula_therm2 - partyintensePSB
save "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\agg02.dta", replace
restore

	*Ciro case study
recode v22c_presvote2 (1=1) (.=.) (else=0), gen(ciro2)
recode v22c_presvote3 (1=1) (.=.) (else=0), gen(ciro3)
recode v11f_mediahgpepresfreq3 1=0 2=15 3=45 4=90 5=180 6=300 18=0 19=0 
replace v11f_mediahgpepresfreq3=0 if v11f_mediahgpepresfreq3==. & respond3==1
recode v11b_mediahgpefreq2 1=1 2=2.5 3=4.5 4=7 8=. 9=.
recode v13b_mediaalldebatesfreq3 8=. 9=.
impute v11f_mediahgpepresfreq3 v11b_mediahgpefreq2, gen(hgpe)
egen tv2=rowmean(v7b1fq_mediatv1choicefreq2 v7b2fq_mediatv2choicefreq2) 

reg mediaattention2 date2 if ciro2==1
reg hgpe date2 if ciro2==1
reg tv2 date2 if ciro2==1

egen hgpestd=std(hgpe)
egen tv2std=std(tv2)

*FIGURE 4.10
ci mean mediaattention2 hgpestd tv2std v13b_mediaalldebatesfreq3 if ciro2==1 & date2<15568
ci mean mediaattention2 hgpestd tv2std v13b_mediaalldebatesfreq3 if ciro2==1 & date2>15567
ci mean mediaattention2 hgpestd tv2std v13b_mediaalldebatesfreq3 if ciro3==1 & ciro2==1

preserve
collapse (mean) mean=ciro2 (count) n=ciro2, by(date2)
restore

*graph bar mediaattention hgpestd tv2std if ciro2==1, by(ciro3) 

*lowess ciro2 date2
	
probit ciro2 c.mediaattention2##c.date2 awarez city1
probit ciro2 c.hgpe##c.date2 awarez city1
probit ciro2 c.tv2##c.date2 awarez city1

preserve
keep id awarez
sort id
save "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\awarez.dta", replace
restore

*----------------------------------------------PARTY CONTACT
recode v38b1b_persuade1frompartyname3 v38b2b_persuade2frompartyname3 v38b3b_persuade3frompartyname3 (4=1) (12=1) (8=1) (9=1) (else=0), gen(lulacontact31 lulacontact32 lulacontact33)
recode v38b1b_persuade1frompartyname3 v38b2b_persuade2frompartyname3 v38b3b_persuade3frompartyname3 (3=1) (1=1) (2=1) (5=1) (else=0), gen(serracontact31 serracontact32 serracontact33)
recode v38b1b_persuade1frompartyname3 v38b2b_persuade2frompartyname3 v38b3b_persuade3frompartyname3 (14=1)  (else=0), gen(agcontact31 agcontact32 agcontact33)
recode v38b1b_persuade1frompartyname3 v38b2b_persuade2frompartyname3 v38b3b_persuade3frompartyname3 (13=1) (6=1) (7=1) (else=0), gen(cirocontact31 cirocontact32 cirocontact33)
recode v38b1b_persuade1frompartyname3 v38b2b_persuade2frompartyname3 v38b3b_persuade3frompartyname3 (10=1) (11=1) (20=1) (21=1) (else=0), gen(othercontact31 othercontact32 othercontact33)

egen totcontact3=rsum(lulacontact31 lulacontact32 lulacontact33 serracontact31 serracontact32 serracontact33 agcontact31 agcontact32 agcontact33 cirocontact31 cirocontact32 cirocontact33 othercontact31 othercontact32 othercontact33)
replace totcontact3=. if respond3~=1
recode totcontact3 0=1 .=. else=0, gen(zerocontact3)

*For chapter 7
nbreg totcontact3 disctotal2 partyintense1 awarez city1 

reg v39a_persuadetoothersyesorno3 awarez city1

*recode partycontact var to match coding of presvote.
recode v38b1b_persuade1frompartyname3 v38b2b_persuade2frompartyname3 v38b3b_persuade3frompartyname3 /// 
(4=2) (12=2) (8=2) (9=2) (13=1) (6=1) (7=1) (3=4) (1=4) (2=4) (5=4) (14=5) (10=16) (11=16) (20=16) (21=16), gen(partycontact31 partycontact32 partycontact33)

	*Agreement and disagreement
gen agreecontact31=0
replace agreecontact31=1 if partycontact31==v22c_presvote2
replace agreecontact31=. if v22c_presvote2>16
replace agreecontact31=. if partycontact31>16
gen agreecontact32=0
replace agreecontact32=1 if partycontact32==v22c_presvote2
replace agreecontact32=. if v22c_presvote2>16
replace agreecontact32=. if partycontact32>16
gen agreecontact33=0
replace agreecontact33=1 if partycontact33==v22c_presvote2
replace agreecontact33=. if v22c_presvote2>16
replace agreecontact33=. if partycontact33>16
replace agreecontact31=. if respond3~=1
replace agreecontact32=. if respond3~=1
replace agreecontact33=. if respond3~=1

egen numagreecontact3=rsum(agreecontact31-agreecontact33)
replace numagreecontact3=. if respond3==0
replace numagreecontact3=. if v22c_presvote2>16

gen disagreementcontact3=1-numagreecontact3/totcontact3
replace disagreementcontact3=0 if totcontact3==0
replace disagreementcontact3=. if v22c_presvote2>16

*------------------------------------------MEDIA DIRECTION
/*
							Lula	Serra	AG	Ciro
TV: Jornal Nacional (79%)	4.24	3.906	4.0588	3.44
TV: Jornal da Record (10%)	3.929	3.8235	3.85	3.26
News Magazine: Veja (23%)	3.7714	2.9666	3.58	2.5033
Paper: Tribuna de Minas 	4.03559	2.6031	3.35	3.3198
Paper: O Globo 				4.1278	3.1911	3.770544	2.7602
Paper: Pioneiro 			4.2619	3.6338	3.3333	2.836364
*/

replace v7b1fq_mediatv1choicefreq3 =v7b1fq_mediatv1choicefreq3 /6
replace v7b2fq_mediatv2choicefreq3 =v7b2fq_mediatv2choicefreq3 /6

replace v8b1fq_mediamags1choicefreq3=v8b1fq_mediamags1choicefreq3/4
replace v8b2fq_mediamags2choicefreq3=v8b2fq_mediamags2choicefreq3/4

replace v9b1fq_mediapaper1choicefreq3=v9b1fq_mediapaper1choicefreq3/7
replace v9b2fq_mediapaper2choicefreq3=v9b2fq_mediapaper2choicefreq3/7

*GENERATE EXPOSURE TO JORNAL NACIONAL
gen JNLula=4.24-(3.906+4.0588+3.44)/3
gen JNSerra=3.906-(4.24+4.0588+3.44)/3
gen JNGaro=4.0588-(3.906+4.24+3.44)/3
gen JNCiro=3.44-(3.906+4.24+4.0588)/3
gen JN=.
replace JN=v7b1fq_mediatv1choicefreq3 *(-JNLula) if v7b1_mediatv1choice3==1 & v22c_presvote2==2
replace JN=v7b2fq_mediatv2choicefreq3 *(-JNLula) if v7b2_mediatv2choice3==1 & v22c_presvote2==2
replace JN=v7b1fq_mediatv1choicefreq3 *(-JNSerra) if v7b1_mediatv1choice3==1 & v22c_presvote2==4
replace JN=v7b2fq_mediatv2choicefreq3 *(-JNSerra) if v7b2_mediatv2choice3==1 & v22c_presvote2==4
replace JN=v7b1fq_mediatv1choicefreq3 *(-JNGaro) if v7b1_mediatv1choice3==1 & v22c_presvote2==5
replace JN=v7b2fq_mediatv2choicefreq3 *(-JNGaro) if v7b2_mediatv2choice3==1 & v22c_presvote2==5
replace JN=v7b1fq_mediatv1choicefreq3 *(-JNCiro) if v7b1_mediatv1choice3==1 & v22c_presvote2==1
replace JN=v7b2fq_mediatv2choicefreq3 *(-JNCiro) if v7b2_mediatv2choice3==1 & v22c_presvote2==1

*GENERATE EXPOSURE TO JORNAL DA RECORD
gen BorisLula=3.929-(3.8235+3.85+3.26)/3
gen BorisSerra=3.8235-(3.929+3.85+3.26)/3
gen BorisGaro=3.85-(3.929+3.8235+3.26)/3
gen BorisCiro=3.26-(3.929+3.8235+3.85)/3
gen Boris=.
replace Boris=v7b1fq_mediatv1choicefreq3 *(-BorisLula) if v7b1_mediatv1choice3==5 & v22c_presvote2==2
replace Boris=v7b2fq_mediatv2choicefreq3 *(-BorisLula) if v7b2_mediatv2choice3==5 & v22c_presvote2==2
replace Boris=v7b1fq_mediatv1choicefreq3 *(-BorisSerra) if v7b1_mediatv1choice3==5 & v22c_presvote2==4
replace Boris=v7b2fq_mediatv2choicefreq3 *(-BorisSerra) if v7b2_mediatv2choice3==5 & v22c_presvote2==4
replace Boris=v7b1fq_mediatv1choicefreq3 *(-BorisGaro) if v7b1_mediatv1choice3==5 & v22c_presvote2==5
replace Boris=v7b2fq_mediatv2choicefreq3 *(-BorisGaro) if v7b2_mediatv2choice3==5 & v22c_presvote2==5
replace Boris=v7b1fq_mediatv1choicefreq3 *(-BorisCiro) if v7b1_mediatv1choice3==5 & v22c_presvote2==1
replace Boris=v7b2fq_mediatv2choicefreq3 *(-BorisCiro) if v7b2_mediatv2choice3==5 & v22c_presvote2==1

*GENERATE EXPOSURE TO VEJA
gen VejaLula=3.7714-(2.9666+3.58+2.5033)/3
gen VejaSerra= 2.9666-(3.7714+3.58+2.5033)/3
gen VejaGaro=3.58-(2.9666+3.7714+2.5033)/3
gen VejaCiro=2.5033-(2.9666+3.7714+ 3.58)/3
gen Veja=.
replace Veja=v8b1fq_mediamags1choicefreq3*(-VejaLula) if v8b1_mediamags1choice3==1 & v22c_presvote2==2
replace Veja=v8b2fq_mediamags2choicefreq3*(-VejaLula) if v8b2_mediamags2choice3==1 & v22c_presvote2==2
replace Veja=v8b1fq_mediamags1choicefreq3*(-VejaSerra) if v8b1_mediamags1choice3==1 & v22c_presvote2==4
replace Veja=v8b2fq_mediamags2choicefreq3*(-VejaSerra) if v8b2_mediamags2choice3==1 & v22c_presvote2==4
replace Veja=v8b1fq_mediamags1choicefreq3*(-VejaGaro) if v8b1_mediamags1choice3==1 & v22c_presvote2==5
replace Veja=v8b2fq_mediamags2choicefreq3*(-VejaGaro) if v8b2_mediamags2choice3==1 & v22c_presvote2==5
replace Veja=v8b1fq_mediamags1choicefreq3*(-VejaCiro) if v8b1_mediamags1choice3==1 & v22c_presvote2==1
replace Veja=v8b2fq_mediamags2choicefreq3*(-VejaCiro) if v8b2_mediamags2choice3==1 & v22c_presvote2==1

*GENERATE EXPOSURE TO TRIBUNA DE MINAS
gen tribunaLula=4.03559	-(2.6031+3.35+3.3198)/3
gen tribunaSerra=2.6031-(4.03559	+3.35+3.3198)/3
gen tribunaGaro=	3.35-(2.6031+4.03559+3.3198)/3
gen tribunaCiro=3.3198	-(2.6031+3.35+4.03559)/3
gen tribuna=.
replace tribuna=v9b1fq_mediapaper1choicefreq3*(-tribunaLula) if v9b1_mediapaper1choice3==7 & v22c_presvote2==2
replace tribuna=v9b2fq_mediapaper2choicefreq3*(-tribunaLula) if v9b2_mediapaper2choice3==7 & v22c_presvote2==2
replace tribuna=v9b1fq_mediapaper1choicefreq3*(-tribunaSerra) if v9b1_mediapaper1choice3==7 & v22c_presvote2==4
replace tribuna=v9b2fq_mediapaper2choicefreq3*(-tribunaSerra) if v9b2_mediapaper2choice3==7 & v22c_presvote2==4
replace tribuna=v9b1fq_mediapaper1choicefreq3*(-tribunaGaro) if v9b1_mediapaper1choice3==7 & v22c_presvote2==5
replace tribuna=v9b2fq_mediapaper2choicefreq3*(-tribunaGaro) if v9b2_mediapaper2choice3==7 & v22c_presvote2==5
replace tribuna=v9b1fq_mediapaper1choicefreq3*(-tribunaCiro) if v9b1_mediapaper1choice3==7 & v22c_presvote2==1
replace tribuna=v9b2fq_mediapaper2choicefreq3*(-tribunaCiro) if v9b2_mediapaper2choice3==7 & v22c_presvote2==1

*GENERATE EXPOSURE TO O GLOBO
gen GloboLula=4.1278-(3.1911+3.770544+2.7602)/3
gen GloboSerra=3.1911-(4.1278+3.770544+2.7602)/3
gen GloboGaro=	3.770544-(4.1278+3.770544+2.7602)/3
gen GloboCiro=2.7602-(3.1911+3.770544+4.1278)/3
gen Globo=.
replace Globo=v9b1fq_mediapaper1choicefreq3*(-GloboLula) if v9b1_mediapaper1choice3==10 & v22c_presvote2==2
replace Globo=v9b2fq_mediapaper2choicefreq3*(-GloboLula) if v9b2_mediapaper2choice3==10 & v22c_presvote2==2
replace Globo=v9b1fq_mediapaper1choicefreq3*(-GloboSerra) if v9b1_mediapaper1choice3==10 & v22c_presvote2==4
replace Globo=v9b2fq_mediapaper2choicefreq3*(-GloboSerra) if v9b2_mediapaper2choice3==10 & v22c_presvote2==4
replace Globo=v9b1fq_mediapaper1choicefreq3*(-GloboGaro) if v9b1_mediapaper1choice3==10 & v22c_presvote2==5
replace Globo=v9b2fq_mediapaper2choicefreq3*(-GloboGaro) if v9b2_mediapaper2choice3==10 & v22c_presvote2==5
replace Globo=v9b1fq_mediapaper1choicefreq3*(-GloboCiro) if v9b1_mediapaper1choice3==10 & v22c_presvote2==1
replace Globo=v9b2fq_mediapaper2choicefreq3*(-GloboCiro) if v9b2_mediapaper2choice3==10 & v22c_presvote2==1

*GENERATE EXPOSURE TO PIONEIRO
gen PioneiroLula=4.2619-(3.6338+3.3333+2.836364)/3
gen PioneiroSerra=3.6338-(4.2619+3.3333+2.836364)/3
gen PioneiroGaro=	3.3333-(3.6338+4.2619+2.836364)/3
gen PioneiroCiro=2.836364-(3.6338+3.3333+4.2619)/3
gen Pioneiro=.
replace Pioneiro=v9b1fq_mediapaper1choicefreq3*(-PioneiroLula) if v9b1_mediapaper1choice3==2 & v22c_presvote2==2
replace Pioneiro=v9b2fq_mediapaper2choicefreq3*(-PioneiroLula) if v9b2_mediapaper2choice3==2 & v22c_presvote2==2
replace Pioneiro=v9b1fq_mediapaper1choicefreq3*(-PioneiroSerra) if v9b1_mediapaper1choice3==2 & v22c_presvote2==4
replace Pioneiro=v9b2fq_mediapaper2choicefreq3*(-PioneiroSerra) if v9b2_mediapaper2choice3==2 & v22c_presvote2==4
replace Pioneiro=v9b1fq_mediapaper1choicefreq3*(-PioneiroGaro) if v9b1_mediapaper1choice3==2 & v22c_presvote2==5
replace Pioneiro=v9b2fq_mediapaper2choicefreq3*(-PioneiroGaro) if v9b2_mediapaper2choice3==2 & v22c_presvote2==5
replace Pioneiro=v9b1fq_mediapaper1choicefreq3*(-PioneiroCiro) if v9b1_mediapaper1choice3==2 & v22c_presvote2==1
replace Pioneiro=v9b2fq_mediapaper2choicefreq3*(-PioneiroCiro) if v9b2_mediapaper2choice3==2 & v22c_presvote2==1

*GEN MEDIA AGREEMENT
egen disagreementmedia3=rsum(JN Boris Veja tribuna Globo Pioneiro) if respond3==1
gen zeromedia3=0 if respond3==1
replace zeromedia3=1 if v7b1fq_mediatv1choicefreq3==0 & v7b2fq_mediatv2choicefreq3==0 & v8b1fq_mediamags1choicefreq3==0 & v8b2fq_mediamags2choicefreq3==0 & v9b1fq_mediapaper1choicefreq3==0 & v9b2fq_mediapaper2choicefreq3==0 & respond3==1
gen othermedia3=0 if respond3==1
replace othermedia3 = 1 if zeromedia==0 & disagreementmedia3==0 & respond3==1

*GENERATE HGPE AGREEMENT
replace v11f_mediahgpepresfreq3=v11f_mediahgpepresfreq3/300
gen hgpedisagree3ciro=ciro2*v11f_mediahgpepresfreq3
gen mediaattention3ciro=ciro2*mediaattention3

/*
gen hgpedisagree3ciro=ciro2*hgpeexposure3

gen hgpedisagree3serra=serra*hgpeexposure3
gen hgpedisagree3garo=garo*hgpeexposure3
gen hgpedisagree3lula=lula*hgpeexposure3
*REDOING THE INTERACTION VARIABLE WITH CIRO
replace hgpeexposure3=0 if ciro==1

*GENERATE OVERALL MEDIA EXPOSURE BY CANDIDATE
gen mediaattention3lula=lula*mediaattention3
gen mediaattention3serra=serra*mediaattention3
gen mediaattention3garo=garo*mediaattention3
*/

*-------------------------------------REGRESSIONS
label variable heterogeneitystrong3 "Disagreeing Alters' diversity (strong)e wave"
label variable heterogeneitystrong2 "Disagreeing Alters' diversity (strong)c wave"
label variable disagreementstrong3 "Network disagreement (strong)e wave"
label variable disagreementstrong2 "Network disagreement (strong)c wave"
label variable zerodiscstrong3 "Singletons (strong)e wave"
label variable zerodiscstrong2 "Singletons (strong)c wave"
label variable awarez "Political awareness"
label variable partyintense1 "Threshold: strength of partisanship"

label variable heterogeneityweak3 "Disagreeing Alters' diversity (weak)e wave"
label variable heterogeneityweak2 "Disagreeing Alters' diversity (weak)c wave"
label variable disagreementweak3 "Network disagreement (weak)e wave"
label variable disagreementweak2 "Network disagreement (weak)c wave"
label variable zerodiscweak2 "Singletons (weak)c wave"
label variable threshold2 "Threshold: candidate evaluations"
label variable disagreementcontact3 "Disagreement of party contactse wave"
label variable zerocontact3 "Not contacted by a partye wave"
label variable disagreementmedia3 "Exposure to Disagreeing Mediae wave"
label variable zeromedia3 "Unexposed to Mediae wave"
label variable othermedia3 "Exposed Only to Uncoded Mediae wave"
label variable hgpedisagree3ciro "Exposure to HGPE among Ciro c wave supporters e wave"
label variable city1 "Juiz de Fora resident"

gen disagreementclient3=1
label variable disagreementclient3 "Benefit from a disagreeing partye wave"
gen zeroclient3=1
label variable zeroclient3 "No benefit from a partye wave"


label variable change23 "DV: Changed vote intention"
recode v22c_presvote2 7=16, gen(presvote2)

*ANALYSES
*this is est1
eststo clear
eststo: logit change23 disagreementstrong3 disagreementstrong2 heterogeneitystrong3 heterogeneitystrong2 zerodiscstrong3 zerodiscstrong2 ///
disagreementcontact3 zerocontact3 hgpedisagree3ciro disagreementmedia3 zeromedia3 othermedia3 city1 i.presvote2 , cluster(bairroibge1)

margins , at(disagreementstrong3==0 disagreementstrong2==0 heterogeneitystrong3==0 heterogeneitystrong2==0 zerodiscstrong3=0 zerodiscstrong2=0) atmeans
margins , at(disagreementstrong3==1 disagreementstrong2==0 heterogeneitystrong3==0 heterogeneitystrong2==0 zerodiscstrong3=0 zerodiscstrong2=0) atmeans

margins , at(disagreementstrong3==0 disagreementstrong2==0 heterogeneitystrong3==0 heterogeneitystrong2==0 zerodiscstrong3=1 zerodiscstrong2=0) atmeans

margins , at(disagreementstrong3==.666 disagreementstrong2==0 heterogeneitystrong3==0 heterogeneitystrong2==0 zerodiscstrong3=0 zerodiscstrong2=0) atmeans
margins , at(disagreementstrong3==.666 disagreementstrong2==0 heterogeneitystrong3==.5 heterogeneitystrong2==0 zerodiscstrong3=0 zerodiscstrong2=0) atmeans

margins , at(disagreementstrong3==1 disagreementstrong2==0 heterogeneitystrong3==0 heterogeneitystrong2==0 zerodiscstrong3=0 zerodiscstrong2=0) atmeans
margins , at(disagreementstrong3==1 disagreementstrong2==0 heterogeneitystrong3==.666 heterogeneitystrong2==0 zerodiscstrong3=0 zerodiscstrong2=0) atmeans

*Figure 4.4A, Table A.4
coefplot (est1, mcolor(black) ciopts(lcolor(black) recast(rcap))), ///
drop(_cons othermedia3 city1 1.presvote2 2.presvote2 4.presvote2 5.presvote2 16.presvote2) xline(0, lcolor(black)) ///
coeflabels(disagreementstrong3="Network.disagreement{sub:{it:e}}" disagreementstrong2="Network.disagreement{sub:{it:c}}" heterogeneitystrong3="Disagreeing.alters'.diversity{sub:{it:e}}" ///
heterogeneitystrong2="Disagreeing.alters'.diversity{sub:{it:c}}" zerodiscstrong3="Singleton{sub:{it:e}}" zerodiscstrong2="Singleton{sub:{it:c}}" disagreementcontact3="Contacted by disagreeing party" ///
zerocontact3="Not contacted by a party" hgpedisagree3ciro="Ciro intention×HEG exposure" disagreementmedia3="Exposure to disagreeing media" zeromedia3="Unexposed to media", labsize(medsmall)) ///
legend(off) graphregion(color(white)) order() plotregion(lstyle(yxline) lcolor(black)) ///
msize(medsmall) groups(disagreementstrong3 disagreementstrong2 heterogeneitystrong3 heterogeneitystrong2 zerodiscstrong3 zerodiscstrong2 disagreementcontact3 zerocontact3 hgpedisagree3ciro disagreementmedia3 zeromedia3 ="{bf:Independent Variables: Political Intermediaries}", labsize(medium)) ///
headings(disagreementstrong3 = "{bf:Horizontal}" disagreementcontact3 = "{bf:Vertical}") ///
xscale(range(-3.5 3.5)) xlab(-3 -2 -1 0 1 2 3) ///
xtitle("{bf:Logit Coefficient}", size(medium)) yscale(titlegap(50))
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2002IndyCoefPlot.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_4A.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_4A.pdf", as(pdf) replace 

*this is est2
*Table A.5
eststo: logit change23 disagreementweak3 disagreementweak2 heterogeneityweak3 heterogeneityweak2 zerodiscweak2 ///
disagreementcontact3 zerocontact3 hgpedisagree3ciro disagreementmedia3 zeromedia3 othermedia3 city1 i.presvote2 

margins , at(disagreementweak3==0 disagreementweak2==0 heterogeneityweak3==0 heterogeneityweak2==0 zerodiscweak2=0) atmeans
margins , at(disagreementweak3==1 disagreementweak2==0 heterogeneityweak3==0 heterogeneityweak2==0 zerodiscweak2=0) atmeans

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
esttab est1 est2 using Brazil2002indy.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Table: Switching in Brazil 2002) ///
modelwidth(6) nonumbers mtitles("1" "2") compress

*/

*---------------------------------
*2006 ELECTION 
*--------------------------------NETWORK AND DISCUSSANT MEASURES-----------------------------------------------
***WAVE 2: DEFINING DISAGREEMENT AND HETEROGENEITY
recode v74c_disc1presvote5 v75c_disc2presvote5 v76c_disc3presvote5 (10=1) (17/99=.) (.=.) (else=0), gen(alck51 alck52 alck53)
recode v74c_disc1presvote5 v75c_disc2presvote5 v76c_disc3presvote5 (2=1) (17/99=.) (.=.) (else=0), gen(lula51 lula52 lula53)
recode v74c_disc1presvote5 v75c_disc2presvote5 v76c_disc3presvote5 (11=1) (17/99=.) (.=.) (else=0), gen(buarque51 buarque52 buarque53)
recode v74c_disc1presvote5 v75c_disc2presvote5 v76c_disc3presvote5 (12=1) (17/99=.) (.=.) (else=0), gen(heloisa51 heloisa52 heloisa53)
recode v74c_disc1presvote5 v75c_disc2presvote5 v76c_disc3presvote5 (13=1) (17/99=.) (.=.) (else=0), gen(eymael51 eymael52 eymael53)
recode v74c_disc1presvote5 v75c_disc2presvote5 v76c_disc3presvote5 (14=1) (17/99=.) (.=.) (else=0), gen(bivar51 bivar52 bivar53)
recode v74c_disc1presvote5 v75c_disc2presvote5 v76c_disc3presvote5 (16=1) (17/99=.) (.=.) (else=0), gen(other51 other52 other53)
recode v74c_disc1presvote5 v75c_disc2presvote5 v76c_disc3presvote5 (17/19=1) (1/16=0) (.=.) (else=0), gen(noop51 noop52 noop53)
egen totopinionateddisc5=rsum(alck51 alck52 alck53 lula51 lula52 lula53 buarque51 buarque52 buarque53 heloisa51 heloisa52 heloisa53 eymael51 eymael52 eymael53 bivar51 bivar52 bivar53 other51 other52 other53)
replace totopinionateddisc5=. if respond5~=1

gen agree51=0
replace agree51=1 if v74c_disc1presvote5==v22c_presvote5
replace agree51=. if v22c_presvote5>16
replace agree51=. if v74c_disc1presvote5>16
gen agree52=0
replace agree52=1 if v75c_disc2presvote5==v22c_presvote5
replace agree52=. if v22c_presvote5>16
replace agree52=. if v75c_disc2presvote5>16
gen agree53=0
replace agree53=1 if v76c_disc3presvote5==v22c_presvote5
replace agree53=. if v22c_presvote5>16
replace agree53=. if v76c_disc3presvote5>16
replace agree51=. if respond5~=1
replace agree52=. if respond5~=1
replace agree53=. if respond5~=1

egen numagree5=rsum(agree51-agree53)
replace numagree5=. if respond5==0
replace numagree5=. if v22c_presvote5>16

		*paradox thing
summ lula51 alck51 heloisa51 buarque51 if city1==1 & totopinionateddisc5>0 & totopinionateddisc5<5 
tab v22c_presvote5 if city1==1 & v22c_presvote5<17

summ lula51 alck51 heloisa51 buarque51 if city1==2 & totopinionateddisc5>0 & totopinionateddisc5<5
tab v22c_presvote5 if city2==2 & v22c_presvote5<17 & v22c_presvote5~=9  

	*STRONG
		*DISAGREEMENT is presence of disagreement over candidates (Sokhey et al AJPS)
				*Note that it only looks at discussants with reported opinions
gen disagreementstrong5=1-numagree5/totopinionateddisc5
replace disagreementstrong5=0 if totopinionateddisc5==0
replace disagreementstrong5=. if v22c_presvote5>16

		*HETEROGENEITY 
recode agree51 0=1 1=0, gen(disagree51)
recode agree52 0=1 1=0, gen(disagree52)
recode agree53 0=1 1=0, gen(disagree53)
egen numdisagree5=rowtotal(disagree51 disagree52 disagree53)
forvalues i=1(1)3 {
replace alck5`i'=. if agree5`i'==1
replace lula5`i'=. if agree5`i'==1
replace buarque5`i'=. if agree5`i'==1
replace heloisa5`i'=. if agree5`i'==1
replace eymael5`i'=. if agree5`i'==1
replace bivar5`i'=. if agree5`i'==1
replace other5`i'=. if agree5`i'==1
}
egen alcksum_numerator5=rsum(alck51 alck52 alck53 )
egen lulasum_numerator5=rsum(lula51 lula52 lula53)
egen buarquesum_numerator5=rsum(buarque51 buarque52 buarque53)
egen heloisasum_numerator5=rsum(heloisa51 heloisa52 heloisa53)
egen eymaelsum_numerator5=rsum(eymael51 eymael52 eymael53)
egen bivarsum_numerator5=rsum(bivar51 bivar52 bivar53)
egen othersum_numerator5=rsum(other51 other52 other53)
gen alckdiscprop5=alcksum_numerator5 / numdisagree5
gen luladiscprop5= lulasum_numerator5 /numdisagree5
gen buarquediscprop5=buarquesum_numerator5 /numdisagree5
gen heloisadiscprop5= heloisasum_numerator5/numdisagree5
gen eymaeldiscprop5=eymaelsum_numerator5/numdisagree5
gen bivardiscprop5= bivarsum_numerator5/numdisagree5
gen otherdiscprop5=othersum_numerator5/numdisagree5
gen heterogeneitystrong5 = 1-(alckdiscprop5^2+luladiscprop5^2+buarquediscprop5^2+heloisadiscprop5^2+eymaeldiscprop5^2+bivardiscprop5^2+otherdiscprop5^2)
recode heterogeneitystrong5 .=0
replace heterogeneitystrong5 =. if numdisagree5<2 & numdisagree5~=. & disagreementstrong5==. 
replace heterogeneitystrong5 =. if respond5==0
drop disagree51 disagree52 disagree53  numdisagree5

		*SINGLETON
recode totopinionateddisc5 0=1 .=. else=0, gen(zerodiscstrong5)
replace zerodiscstrong5=. if respond5==0

	*WEAK
		*DISAGREEMENT is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak5=1-numagree5/disctotal5
replace disagreementweak5=0 if disctotal5==0
replace disagreementweak5=. if v22c_presvote5>16
replace disagreementweak5=. if respond5==0

		*HETEROGENEITY 
replace agree51=0 if v74c_disc1presvote5>16 & v74_disc1named5==1 & v22c_presvote5<17
replace agree52=0 if v75c_disc2presvote5>16 & v75_disc2named5==1 & v22c_presvote5<17
replace agree53=0 if v76c_disc3presvote5>16 & v76_disc3named5==1 & v22c_presvote5<17

recode agree51 0=1 1=0, gen(disagree51)
recode agree52 0=1 1=0, gen(disagree52)
recode agree53 0=1 1=0, gen(disagree53)

egen numdisagree5=rowtotal(disagree51 disagree52 disagree53)
forvalues i=1(1)3 {
replace noop5`i'=. if disagree5`i'==0  
}

egen noopsum_numerator5=rsum(noop51 noop52 noop53)
gen alckdiscprop5weak=alcksum_numerator5 / numdisagree5
gen luladiscprop5weak= lulasum_numerator5 /numdisagree5
gen buarquediscprop5weak=buarquesum_numerator5 /numdisagree5
gen heloisadiscprop5weak= heloisasum_numerator5/numdisagree5
gen eymaeldiscprop5weak=eymaelsum_numerator5/numdisagree5
gen bivardiscprop5weak= bivarsum_numerator5/numdisagree5
gen otherdiscprop5weak=othersum_numerator5/numdisagree5
gen noopdiscprop5weak=noopsum_numerator5/numdisagree5
gen heterogeneityweak5 = 1-(alckdiscprop5weak^2+luladiscprop5weak^2+buarquediscprop5weak^2+heloisadiscprop5weak^2+eymaeldiscprop5weak^2+bivardiscprop5weak^2+otherdiscprop5weak^2+noopdiscprop5weak^2)
recode heterogeneityweak5 .=0
replace heterogeneityweak5 =. if numdisagree5<2 & numdisagree5~=. & disagreementweak5==. 
replace heterogeneityweak5=. if respond5==0

		*SINGLETONS
recode disctotal5 0=1 .=. else=0, gen(zerodiscweak5)
replace zerodiscweak5=. if respond5==0

***WAVE 6: DEFINING DISAGREEMENT AND HETEROGENEITY
recode v74c_disc1presvote6 v75c_disc2presvote6 v76c_disc3presvote6 (10=1) (17/99=.) (.=.) (else=0), gen(alck61 alck62 alck63)
recode v74c_disc1presvote6 v75c_disc2presvote6 v76c_disc3presvote6 (2=1) (17/99=.) (.=.) (else=0), gen(lula61 lula62 lula63)
recode v74c_disc1presvote6 v75c_disc2presvote6 v76c_disc3presvote6 (11=1) (17/99=.) (.=.) (else=0), gen(buarque61 buarque62 buarque63)
recode v74c_disc1presvote6 v75c_disc2presvote6 v76c_disc3presvote6 (12=1) (17/99=.) (.=.) (else=0), gen(heloisa61 heloisa62 heloisa63)
recode v74c_disc1presvote6 v75c_disc2presvote6 v76c_disc3presvote6 (13=1) (17/99=.) (.=.) (else=0), gen(eymael61 eymael62 eymael63)
recode v74c_disc1presvote6 v75c_disc2presvote6 v76c_disc3presvote6 (14=1) (17/99=.) (.=.) (else=0), gen(bivar61 bivar62 bivar63)
recode v74c_disc1presvote6 v75c_disc2presvote6 v76c_disc3presvote6 (16=1) (17/99=.) (.=.) (else=0), gen(other61 other62 other63)
recode v74c_disc1presvote6 v75c_disc2presvote6 v76c_disc3presvote6 (17/19=1) (1/16=0) (.=.) (else=0), gen(noop61 noop62 noop63)
egen totopinionateddisc6=rsum(alck61 alck62 alck63 lula61 lula62 lula63 buarque61 buarque62 buarque63 heloisa61 heloisa62 heloisa63 eymael61 eymael62 eymael63 bivar61 bivar62 bivar63 other61 other62 other63)
replace totopinionateddisc6=. if respond6~=1

				*Note this uses wave 5 MR vote choice
gen agree61=0
replace agree61=1 if v74c_disc1presvote6==v22c_presvote5
replace agree61=. if v22c_presvote5>16
replace agree61=. if v74c_disc1presvote6>16
gen agree62=0
replace agree62=1 if v75c_disc2presvote6==v22c_presvote5
replace agree62=. if v22c_presvote5>16
replace agree62=. if v75c_disc2presvote6>16
gen agree63=0
replace agree63=1 if v76c_disc3presvote6==v22c_presvote5
replace agree63=. if v22c_presvote5>16
replace agree63=. if v76c_disc3presvote6>16
replace agree61=. if respond6~=1
replace agree62=. if respond6~=1
replace agree63=. if respond6~=1
egen numagree6=rsum(agree61-agree63)
replace numagree6=. if respond6==0
replace numagree6=. if v22c_presvote5>16

	*STRONG
		*DISAGREEMENT is presence of disagreement over candidates (Sokhey et al AJPS)
				*Note that it only looks at discussants with reported opinions
gen disagreementstrong6=1-numagree6/totopinionateddisc6
replace disagreementstrong6=0 if totopinionateddisc6==0
replace disagreementstrong6=. if v22c_presvote5>16
replace disagreementstrong6=. if respond6==0
replace disagreementstrong6=. if respond5==0

		*HETEROGENEITY 
recode agree61 0=1 1=0, gen(disagree61)
recode agree62 0=1 1=0, gen(disagree62)
recode agree63 0=1 1=0, gen(disagree63)
egen numdisagree6=rowtotal(disagree61 disagree62 disagree63)
forvalues i=1(1)3 {
replace alck6`i'=. if agree6`i'==1
replace lula6`i'=. if agree6`i'==1
replace buarque6`i'=. if agree6`i'==1
replace heloisa6`i'=. if agree6`i'==1
replace eymael6`i'=. if agree6`i'==1
replace bivar6`i'=. if agree6`i'==1
replace other6`i'=. if agree6`i'==1
}

egen alcksum_numerator6=rsum(alck61 alck62 alck63 )
egen lulasum_numerator6=rsum(lula61 lula62 lula63)
egen buarquesum_numerator6=rsum(buarque61 buarque62 buarque63)
egen heloisasum_numerator6=rsum(heloisa61 heloisa62 heloisa63)
egen eymaelsum_numerator6=rsum(eymael61 eymael62 eymael63)
egen bivarsum_numerator6=rsum(bivar61 bivar62 bivar63)
egen othersum_numerator6=rsum(other61 other62 other63)
gen alckdiscprop6=alcksum_numerator6 / numdisagree6
gen luladiscprop6= lulasum_numerator6 /numdisagree6
gen buarquediscprop6=buarquesum_numerator6 /numdisagree6
gen heloisadiscprop6= heloisasum_numerator6/numdisagree6
gen eymaeldiscprop6=eymaelsum_numerator6/numdisagree6
gen bivardiscprop6= bivarsum_numerator6/numdisagree6
gen otherdiscprop6=othersum_numerator6/numdisagree6
gen heterogeneitystrong6 = 1-(alckdiscprop6^2+luladiscprop6^2+buarquediscprop6^2+heloisadiscprop6^2+eymaeldiscprop6^2+bivardiscprop6^2+otherdiscprop6^2)
recode heterogeneitystrong6 .=0
replace heterogeneitystrong6 =. if numdisagree6<2 & numdisagree6~=. & disagreementstrong6==. 
replace heterogeneitystrong6 =. if respond5==0
replace heterogeneitystrong6 =. if respond6==0
drop disagree61 disagree62 disagree63  numdisagree6

		*SINGLETONS
recode totopinionateddisc6 0=1 .=. else=0, gen(zerodiscstrong6)
replace zerodiscstrong6=. if respond6==0
replace zerodiscstrong6=. if respond5==0

	*WEAK 
		*DISAGREEMENT is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak6=1-numagree6/disctotal6
replace disagreementweak6=0 if disctotal6==0
replace disagreementweak6=. if v22c_presvote5>16

		*HETEROGENEITY 
replace agree61=0 if v74c_disc1presvote6>16 & v74_disc1named5==1 & v22c_presvote5<17
replace agree62=0 if v75c_disc2presvote6>16 & v75_disc2named5==1 & v22c_presvote5<17
replace agree63=0 if v76c_disc3presvote6>16 & v76_disc3named5==1 & v22c_presvote5<17

recode agree61 0=1 1=0, gen(disagree61)
recode agree62 0=1 1=0, gen(disagree62)
recode agree63 0=1 1=0, gen(disagree63)

egen numdisagree6=rowtotal(disagree61 disagree62 disagree63)
forvalues i=1(1)3 {
replace noop6`i'=. if disagree6`i'==0  
}

egen noopsum_numerator6=rsum(noop61 noop62 noop63)
gen alckdiscprop6weak=alcksum_numerator6 / numdisagree6
gen luladiscprop6weak= lulasum_numerator6 /numdisagree6
gen buarquediscprop6weak=buarquesum_numerator6 /numdisagree6
gen heloisadiscprop6weak= heloisasum_numerator6/numdisagree6
gen eymaeldiscprop6weak=eymaelsum_numerator6/numdisagree6
gen bivardiscprop6weak= bivarsum_numerator6/numdisagree6
gen otherdiscprop6weak=othersum_numerator6/numdisagree6
gen noopdiscprop6weak=noopsum_numerator6/numdisagree6
gen heterogeneityweak6 = 1-(alckdiscprop6weak^2+luladiscprop6weak^2+buarquediscprop6weak^2+heloisadiscprop6weak^2+eymaeldiscprop6weak^2+bivardiscprop6weak^2+otherdiscprop6weak^2+noopdiscprop6weak^2)
recode heterogeneityweak6 .=0
replace heterogeneityweak6 =. if numdisagree6<2 & numdisagree6~=. & disagreementstrong6==. 
replace heterogeneityweak6 =. if respond6==0
drop disagree61 disagree62 disagree63  numdisagree6
replace heterogeneityweak6=. if respond6==0
replace heterogeneityweak6=. if respond5==0

		*SINGLETONS
*don't need because no change between 5 and 6. Just use zerodiscweak5

*-----------------GENERATE MEDIA ATTENTION SCORE (IV). 
replace v7b1fq_mediatv1choicefreq6=0 if v7a_mediatvyesorno6==2
replace v7b2fq_mediatv2choicefreq6=0 if v7a_mediatvyesorno6==2
replace v8b1fq_mediamags1choicefreq6=0 if v8a_mediamagsyesorno6==2
replace v8b2fq_mediamags2choicefreq6=0 if v8a_mediamagsyesorno6==2
replace v9b1fq_mediapaper1choicefreq6=0 if v9a_mediapaperyesorno6==2
replace v9b2fq_mediapaper2choicefreq6=0 if v9a_mediapaperyesorno6==2
recode v7b1fq_mediatv1choicefreq6 v7b2fq_mediatv2choicefreq6 v8b1fq_mediamags1choicefreq6 v8b2fq_mediamags2choicefreq6 v9b1fq_mediapaper1choicefreq6 v9b2fq_mediapaper2choicefreq6 (.=0)
mvdecode v7b1fq_mediatv1choicefreq6 v7b2fq_mediatv2choicefreq6 v8b1fq_mediamags1choicefreq6 v8b2fq_mediamags2choicefreq6 v9b1fq_mediapaper1choicefreq6 v9b2fq_mediapaper2choicefreq6, mv(8)
mvdecode v7b1fq_mediatv1choicefreq6 v7b2fq_mediatv2choicefreq6 v8b1fq_mediamags1choicefreq6 v8b2fq_mediamags2choicefreq6 v9b1fq_mediapaper1choicefreq6 v9b2fq_mediapaper2choicefreq6, mv(9)
factor  v7b1fq_mediatv1choicefreq6 v7b2fq_mediatv2choicefreq6 v8b1fq_mediamags1choicefreq6 v8b2fq_mediamags2choicefreq6 v9b1fq_mediapaper1choicefreq6 v9b2fq_mediapaper2choicefreq6 if respond6==1, pcf factors(1)
predict mediaattention6temp
impute mediaattention6temp v7b1fq_mediatv1choicefreq6 v7b2fq_mediatv2choicefreq6 v8b1fq_mediamags1choicefreq6 v8b2fq_mediamags2choicefreq6 v9b1fq_mediapaper1choicefreq6 v9b2fq_mediapaper2choicefreq6 if respond6==1, gen(mediaattention6)

*PARTY CONTACT
recode v38b1b_persuade1frompartyname6 v38b2b_persuade2frompartyname6 v38b3b_persuade3frompartyname6 (1=1) (4=1) (5=1) (8=1) (12=1) (14=1) (else=0), gen(lulacontact61 lulacontact62 lulacontact63)
recode v38b1b_persuade1frompartyname6 v38b2b_persuade2frompartyname6 v38b3b_persuade3frompartyname6 (2=1) (3=1) (13=1) (else=0), gen(alckmincontact61 alckmincontact62 alckmincontact63)
recode v38b1b_persuade1frompartyname6 v38b2b_persuade2frompartyname6 v38b3b_persuade3frompartyname6 (6=1) (else=0), gen(buarquecontact61 buarquecontact62 buarquecontact63)
recode v38b1b_persuade1frompartyname6 v38b2b_persuade2frompartyname6 v38b3b_persuade3frompartyname6 (10=1) (else=0), gen(heloisacontact61 heloisacontact62 heloisacontact63)
recode v38b1b_persuade1frompartyname6 v38b2b_persuade2frompartyname6 v38b3b_persuade3frompartyname6 (7=1) (9=1) (11=1) (20=1) (21=1) (else=0), gen(othercontact61 othercontact62 othercontact63)

egen totcontact6=rsum(lulacontact61 lulacontact62 lulacontact63 alckmincontact61 alckmincontact62 alckmincontact63 buarquecontact61 buarquecontact62 buarquecontact63 heloisacontact61 heloisacontact62 heloisacontact63 othercontact61 othercontact62 othercontact63)
replace totcontact6=. if respond6~=1
recode totcontact6 0=1 .=. else=0, gen(zerocontact6)

*recode partycontact var to match coding of presvote.
recode v38b1b_persuade1frompartyname6 v38b2b_persuade2frompartyname6 v38b3b_persuade3frompartyname6 /// 
(1=2) (4=2) (5=2) (8=2) (12=2) (14=2) (2=10) (3=10) (13=10) (6=11) (10=12) (7=16) (9=16) (11=16) (20=16) (21=16), gen(partycontact61 partycontact62 partycontact63)

		*Agreement and disagreement
gen agreecontact61=0
replace agreecontact61=1 if partycontact61==v22c_presvote5
replace agreecontact61=. if v22c_presvote5>16
replace agreecontact61=. if partycontact61>16
gen agreecontact62=0
replace agreecontact62=1 if partycontact62==v22c_presvote5
replace agreecontact62=. if v22c_presvote5>16
replace agreecontact62=. if partycontact62>16
gen agreecontact63=0
replace agreecontact63=1 if partycontact63==v22c_presvote5
replace agreecontact63=. if v22c_presvote5>16
replace agreecontact63=. if partycontact63>16
replace agreecontact61=. if respond6~=1
replace agreecontact62=. if respond6~=1
replace agreecontact63=. if respond6~=1

egen numagreecontact6=rsum(agreecontact61-agreecontact63)
replace numagreecontact6=. if respond6==0
replace numagreecontact6=. if v22c_presvote5>16

			*Strong disagreement is presence of disagreement over candidates (Sokhey et al AJPS)
gen disagreementcontact6=1-numagreecontact6/totcontact6
replace disagreementcontact6=0 if totcontact6==0
replace disagreementcontact6=. if v22c_presvote5>16

	*GET QUICK CROSSTABS
preserve
replace disagreementstrong5=-1 if zerodiscstrong5==1
tab disctotal5 if respond5==1 & v22c_presvote5<16
tab disagreementstrong5 if respond5==1 & v22c_presvote5<16
tab disagreementstrong5 heterogeneitystrong5 if respond5==1  & v22c_presvote5<16, cel
restore

*PARTISAN INTENSITY
recode  v43a_pidyesorno5 2=0 .=0 1=1 else=0
replace v43a_pidyesorno5 =0 if v43b_pidparty5==.
replace v43a_pidyesorno5 =0 if v43b_pidparty5==21

recode  v44a_pidyesorno5 2=0 .=0 1=1 else=0
replace v44a_pidyesorno5=. if v43a_pidyesorno5==1
replace v44a_pidyesorno5 =0 if v44b_pidparty5==.
replace v44a_pidyesorno5 =0 if v44b_pidparty5==21

gen partyintense5=0
replace partyintense5=1 if v43a_pidyesorno5==1
replace partyintense5=.5 if v44a_pidyesorno5==1

*POLITICAL AWARENESS
drop correct*
gen correct11cs=0 if respond5==1 & city1==1
replace correct11cs=1 if s14hcs_know8cargopauletti5==3 & correct11cs==0 
gen correct12=0 if respond5==1
replace correct12=1 if s14bW4_know2vicepres5==4 & respond5==1
gen correct14=0 if respond5==1
replace correct14=1 if s14d_know4mercosul5==2 & respond5==1
gen correct16=0 if respond5==1
replace correct16=1 if s14fW5_know6preschamber5==4 & respond5==1
gen correct15cs=0 if respond5==1 & city1==1
replace correct15cs=1 if s14ecs_know5senator5==1 & correct15cs==0
gen correct15jf=0 if respond5==1 & city1==2
replace correct15jf=1 if s14ejf_know5senator5==1 & correct15jf==0
gen correct17cs=0 if respond5==1 & city1==1
replace correct17cs=1 if s14gcs_know7mayor5==3
gen correct17jf=0 if respond5==1 & city1==2
replace correct17jf=1 if s14gjf_know7mayor5==1
gen correct13cs=0 if respond5==1 & city1==1
replace correct13cs=1 if s14ics_know9simonparty5==2

irt 2pl correct11cs correct12 correct14 correct15cs correct15jf correct16 correct17cs correct17jf correct13cs if respond5==1
predict awaretemp5 if respond5==1, latent ebmeans
egen awarez5=std(awaretemp5) if respond5==1

preserve
keep id awarez awarez5
sort id 
save "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\awarez.dta", replace
restore

bysort change56: summ awarez5 
bysort change56 zerodiscstrong5: summ disagreementstrong5 
bysort change56 zerodiscweak5: summ disagreementweak5

*eststo clear
*Figure 4.9B, Table A.11
logit change56 awarez5 i.v22c_presvote5 city1 , cluster(bairroibge1)
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans

logit switch56 awarez5 city1 , cluster(bairroibge1)
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans

ci mean awarez5 if v22c_presvote6==2 & switch56==0
ci mean awarez5 if v22c_presvote6==2 & switch56==1

ci mean awarez5 if v22c_presvote6==10 & switch56==0
ci mean awarez5 if v22c_presvote6==10 & switch56==1

ci mean awarez5 if v22c_presvote6==12 & switch56==0
ci mean awarez5 if v22c_presvote6==12 & switch56==1

	*timing
gen timing06=2 if v22c_presvote5==v22c_presvote6 & v22c_presvote5~=. & v22c_presvote6~=. 
replace timing06=3 if v22c_presvote5~=v22c_presvote6 & v22c_presvote5~=. & v22c_presvote6~=.  
replace timing06=. if v22c_presvote6>16 
replace timing06=. if respond5==. | respond6==.
replace timing06=. if v22c_presvote5==9

*Table A.11
ologit timing06 awarez5 city6, cluster(bairroibge1)
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans
margin, at((min) awarez) atmeans
margin, at((max) awarez) atmeans

*Figure 4.9A
margin, at((max) awarez) at((min) awarez) atmeans
xyz
/*
marginsplot, xdimension(_outcome) yscale(range(0 .6)) ylab(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%") xscale(range(.8 3.2)) ///
ytitle("{bf:Model-Predicted}" "{bf:Percentage of Voters}") legend(off) graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) xlab(1 " " 2 "Bef. mid-campaign" 3 "Bef. election day", labsize(small)) xtitle("{bf:Timing of Vote Decision}") ///
title("{bf:       Brazil 2006}", size(medsmall) color(black)) plot1opts(msymbol(0) lcolor(black) mcolor(black) ) plot2opts(msymbol(S) lcolor(black) mcolor(black) ) ci1opts(color(black)) ci2opts(color(black)) ///
text(.695 2.395 "Max knowledge", size(small)) text(.305 2.39 "Min knowledge", color(black) size(small)) text(0 1 "{it:N}=1,513", size(small)) 
graph save "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2006timing.gph", replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2006timing.tif", as(tif) replace
*/
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
*esttab est1 est2 using figure49b.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Vote Switching by Political Knowledge)

*eststo clear
label variable disagreementstrong5 "Network disagreement"
label variable heterogeneitystrong5 "Disagreeing alters’ diversity"
label variable zerodiscstrong5 "Singleton"
label variable city1 "Juiz de Fora"

*eststo: logit change56 disagreementstrong5 heterogeneitystrong5 zerodiscstrong5 city1, cluster(bairroibge1)
*Figure 4.3, Table A.3
logit change56 disagreementstrong5 heterogeneitystrong5 zerodiscstrong5 city1, cluster(bairroibge1)
margin, at(disagreementstrong5=1 zerodiscstrong5==0 heterogeneitystrong5==0)
margin, at(disagreementstrong5=1 zerodiscstrong5==0 heterogeneitystrong5==.4444444)
margin, at(disagreementstrong5=.666666666 zerodiscstrong5==0 heterogeneitystrong5==0)
margin, at(disagreementstrong5=0 zerodiscstrong5==1 heterogeneitystrong5==0)
margin, at(disagreementstrong5=0 zerodiscstrong5==0 heterogeneitystrong5==0)

*esttab est1 using figure43b.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Changed Vote Preference between Campaign and Election Waves by Campaign-Wave Network Traits: Results from Four Binary Logit Models )

*THRESHOLD (note this introduces some missing data)
mvdecode v41a_thermlula* v41c_thermciro* v41d_thermgaro* v41e_thermserra* v41x_thermalckmin* v41y_thermheloisa* v41z_thermbuarque* v42a_pidthermpsdb* v42c_pidthermpt* v42e_pidthermpsb* v42f_pidthermpps*, mv(18)
mvdecode v41a_thermlula* v41c_thermciro* v41d_thermgaro* v41e_thermserra* v41x_thermalckmin* v41y_thermheloisa* v41z_thermbuarque* v42a_pidthermpsdb* v42c_pidthermpt* v42e_pidthermpsb* v42f_pidthermpps*, mv(19)

	*For Lula voters
egen tempalckcristhelena=rowmean(v41x_thermalckmin5 v41y_thermheloisa5)
egen templula5=rowmean(v41a_thermlula5)
gen lula_therm5=templula5-tempalckcristhelena

	*For Alckmin voters
egen templulacristhelena=rowmean(v41a_thermlula5 v41y_thermheloisa5)
egen tempalckmin=rowmean(v41x_thermalckmin5 )
gen alckmin_therm=tempalckmin-templulacristhelena

	*For Buarque voters
egen templulaaclkhelena=rowmean(v41a_thermlula5 v41y_thermheloisa5 v41x_thermalckmin5)
egen tempbuarque=rowmean(v41z_thermbuarque5)
gen buarque_therm=tempbuarque-templulaaclkhelena

	*For Helena voters
egen templulaalckbuarque=rowmean(v41a_thermlula5 v41x_thermalckmin5)
egen temphelena=rowmean(v41y_thermheloisa5)
gen helena_therm=temphelena-templulaalckbuarque

gen threshold5=lula_therm5 if v22c_presvote5==2
replace threshold5=alckmin_therm if v22c_presvote5==10
replace threshold5=buarque_therm if v22c_presvote5==11
replace threshold5=helena_therm if v22c_presvote5==12
*replace threshold5=0 if threshold5<0
replace threshold5=(threshold5+10)/20

label variable heterogeneitystrong6 "Alters' diversity (strong)e wave"
label variable heterogeneitystrong5 "Alters' diversity (strong)c wave"
label variable disagreementstrong6 "Network disagreement (strong)e wave"
label variable disagreementstrong5 "Network disagreement (strong)c wave"
label variable zerodiscstrong6 "Isolates (strong)e wave"
label variable zerodiscstrong5 "Isolates (strong)c wave"
label variable awarez "Political awareness"
label variable partyintense5 "Threshold: strength of partisanship"

label variable heterogeneityweak6 "Alters' diversity (weak)e wave"
label variable heterogeneityweak5 "Alters' diversity (weak)c wave"
label variable disagreementweak6 "Network disagreement (weak)e wave"
label variable disagreementweak5 "Network disagreement (weak)c wave"
*label variable zerodiscweak6 "Isolates (weak)e wave"
label variable zerodiscweak5 "Isolates (weak)c wave"
label variable threshold5 "Threshold: candidate evaluations"
label variable disagreementcontact6 "Disagreement of party contactse wave"
label variable zerocontact6 "Not contacted by a partye wave"
label variable city1 "Juiz de Fora resident"

label variable change56 "DV: Changed vote intention"

*ANALYSES
preserve
	*PARTISAN INTENSITY
recode v43b_pidparty5 (4=1) (else=0), gen(partyintensePT5)
replace partyintensePT5=.5 if v44b_pidparty5==4
replace partyintensePT5=. if respond5==0
egen partyintensePT=mean(partyintensePT5), by(city1)

recode v43b_pidparty5 (3=1) (else=0), gen(partyintensePSDB5)
replace partyintensePSDB5=.5 if v44b_pidparty5==3
replace partyintensePSDB5=. if respond5==0
egen partyintensePSDB=mean(partyintensePSDB5), by(city1)

recode v43b_pidparty5 (22=1) (else=0), gen(partyintensePSOL5)
replace partyintensePSOL5=.5 if v44b_pidparty5==22
replace partyintensePSOL5=. if respond5==0
egen partyintensePSOL=mean(partyintensePSOL5), by(city1)

	*Stability
recode v22c_presvote5 v22c_presvote6 (17/19=.), gen(presvote5 presvote6) 
label values presvote5 presvote6 presvote
bysort city1: tab presvote5 presvote6, row
bysort city1 presvote5: ci means disagreementstrong5

gen lula06stability=0 if presvote5==2 & presvote6~=.
replace lula06stability=1 if presvote5==2 & presvote6==2

gen alckmin06stability=0 if presvote5==10 & presvote6~=.
replace alckmin06stability=1 if presvote5==10 & presvote6==10

gen helena06stability=0 if presvote5==12 & presvote6~=.
replace helena06stability=1 if presvote5==12 & presvote6==12

*to get complement of their vote total in campaign wave
recode presvote5 2=1 .=. else=0, gen(lulavotetemp1)
egen lulavotetemp2=mean(lulavotetemp1), by(city1)
gen lulavote=1-lulavotetemp2

recode presvote5 10=1 .=. else=0, gen(alckminvotetemp1)
egen alckminvotetemp2=mean(alckminvotetemp1), by(city1)
gen alckminvote=1-alckminvotetemp2

recode presvote5 12=1 .=. else=0, gen(helenavotetemp1)
egen helenavotetemp2=mean(helenavotetemp1), by(city1)
gen helenavote=1-helenavotetemp2

replace lula_therm5=. if v22c_presvote5~=2
replace alckmin_therm=. if v22c_presvote5~=10
replace helena_therm=. if v22c_presvote5~=12
replace lulavote=. if v22c_presvote5~=2
replace alckminvote=. if v22c_presvote5~=10
replace helenavote=. if v22c_presvote5~=12
replace partyintensePT=. if v22c_presvote5~=2
replace partyintensePSDB=. if v22c_presvote5~=10
replace partyintensePSOL=. if v22c_presvote5~=12

label drop presvote
label define presvote  1 Ciro 2 Lula 3 Roseana 4 Serra 5 Garotinho 6 Itamar 7 "Jose Maria" 8 "Rui Costa Pimenta" ///
9 "Fernando Henrique Cardoso" 10 Alckmin 11 Buarque 12 "Heloisa Helena" 13 "Eymeal" 14 "Bivar" 17 "No One" 18 DK 19 NR
label values presvote5 presvote6 presvote

collapse disagreementstrong5 lula_therm5 alckmin_therm helena_therm lulavote alckminvote helenavote lula06stability alckmin06stability helena06stability partyintensePT partyintensePSDB partyintensePSOL, by(presvote5 city1)

drop if presvote5==9 | presvote5==11 | presvote5>12
egen threshold5=rowmean(lula_therm5 alckmin_therm helena_therm)
egen votecomplement=rowmean(lulavote alckminvote helenavote )
egen stability=rowmean(lula06stability alckmin06stability helena06stability )
egen partyintense=rowmean(partyintensePT partyintensePT partyintensePSDB partyintensePSOL)
drop lula_therm5 - partyintensePSOL
gen year=2006
rename presvote5 presvote
rename threshold5 threshold
rename disagreementstrong5 disagreementstrong
save "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\agg06.dta", replace
restore

ttest disagreementweak5==disagreementweak6 
ttest disagreementstrong5==disagreementstrong6 

bysort v22c_presvote5: summ disagreementstrong5 disagreementweak5
recode v22c_presvote5 11=16 13/14=16, gen(presvote5)

	*Listwise deletion
eststo clear
eststo: logit change56 disagreementstrong6 disagreementstrong5 heterogeneitystrong6 heterogeneitystrong5 zerodiscstrong6 zerodiscstrong5 ///
disagreementcontact6 zerocontact6 city1 i.presvote5##c.mediaattention6, cluster(bairroibge1)
margins , at(disagreementstrong6==0 disagreementstrong5==0 heterogeneitystrong6==0 heterogeneitystrong5==0 zerodiscstrong6=0 zerodiscstrong5=0) atmeans
margins , at(disagreementstrong6==1 disagreementstrong5==0 heterogeneitystrong6==0 heterogeneitystrong5==0 zerodiscstrong6=0 zerodiscstrong5=0) atmeans
margins , at(disagreementstrong6==0 disagreementstrong5==0 heterogeneitystrong6==0 heterogeneitystrong5==0 zerodiscstrong6=1 zerodiscstrong5=0) atmeans

*Figure 4.4B, Table A.4
coefplot (est1 , mcolor(black) ciopts(lcolor(black) recast(rcap))), ///
drop(_cons othermedia3 city1 10.presvote5 2.presvote5 16.presvote5 12.presvote5) xline(0, lcolor(black)) ///
coeflabels(disagreementstrong6="Network.disagreement{sub:{it:e}}" disagreementstrong5="Network.disagreement{sub:{it:c}}" heterogeneitystrong6="Disagreeing.alters'.diversity{sub:{it:e}}" ///
heterogeneitystrong5="Disagreeing.alters'.diversity{sub:{it:c}}" zerodiscstrong6="Singleton{sub:{it:e}}" zerodiscstrong5="Singleton{sub:{it:c}}" disagreementcontact6="Contacted by disagreeing party" ///
zerocontact6="Not contacted by a party" 10.presvote5#c.mediaattention6="Alckmin intention×Media exposure" mediaattention6="Media exposure" ///
16.presvote5#c.mediaattention6="Other intention×Media exposure" 12.presvote5#c.mediaattention6="HH intention×Media exposure", labsize(medsmall)) ///
legend(off) graphregion(color(white)) order() plotregion(lstyle(yxline) lcolor(black)) ///
msize(medsmall) groups(disagreementstrong6 disagreementstrong5 heterogeneitystrong6 heterogeneitystrong5 zerodiscstrong6 zerodiscstrong5 disagreementcontact6 zerocontact6 mediaattention6 ///
10.presvote5#c.mediaattention6 16.presvote5#c.mediaattention6  12.presvote5#c.mediaattention6 ="{bf:Independent Variables: Political Intermediaries}", labsize(medium)) ///
headings(disagreementstrong6 = "{bf:Horizontal}" disagreementcontact6 = "{bf:Vertical}") ///
xscale(range(-3.5 3.5)) xlab(-3 -2 -1 0 1 2 3) ///
xtitle("{bf:Logit Coefficient}", size(medium)) yscale(titlegap(55))
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2006IndyCoefPlot.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_4B.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_4B.pdf", as(pdf) replace 

*Table A.5
eststo: logit change56 disagreementweak6 disagreementweak5 heterogeneityweak6 heterogeneityweak5 zerodiscweak5 ///
disagreementcontact6 zerocontact6 city1 i.presvote5##c.mediaattention6
margins , at(disagreementweak6==0 disagreementweak5==0 heterogeneityweak6==0 heterogeneityweak5==0 zerodiscweak5=0) atmeans
margins , at(disagreementweak6==1 disagreementweak5==0 heterogeneityweak6==0 heterogeneityweak5==0 zerodiscweak5=0) atmeans

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
esttab est1 est2 using Brazil2006indy.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Table: Switching in Brazil 2006) ///
modelwidth(6) nonumbers mtitles("1" "2") compress

